private boolean shouldBeDirect(final Object target) { return expectedInstance == target || (target instanceof Class<?> && withStatics) // static call, we do not instrument Class || (includeList != null && isClassIncluded(target.getClass().getName())); }
@Override public boolean checkForChange(DirectCallPolicy previousPolicy) { // first setup if (previousPolicy == NOP) { return true; } // twice setup if (previousPolicy instanceof OneShotDirectCallPolicy) { throw new DirectCallException("already expecting a direct call on <" + ((OneShotDirectCallPolicy) previousPolicy).expectedInstance + "> but here's a new request for <" + expectedInstance + ">"); } // we are inside full stack direct call => do not change anything if (previousPolicy instanceof FullStackDirectCallPolicy && ((FullStackDirectCallPolicy) previousPolicy).checkWeAreDeepInsideStack()) { return false; } // unexpected throw new DirectCallException("Direct call policy is already set to " + previousPolicy); } }
@Override public DirectCallPolicy onMethodInvocationFinished(Object target) { if (expectedInstance == null) { // error happened return NOP; } if (depth < 0) { throw new DirectCallException("Stack depth is negative: " + depth + ", target: " + expectedInstance); } if (shouldBeDirect(target) && depth-- == 0) { return NOP; } return this; }
public FullStackDirectCallPolicy create() { return new FullStackDirectCallPolicy(target, statics, includeList); }
public static FullStackDirectCallPolicy withTarget(final Object target) { return build(target).create(); }
public static <T> T directlyOnFullStack(T shadowedObject) { setupDirectCallPolicy(FullStackDirectCallPolicy.withTarget(shadowedObject)); return shadowedObject; }
@Override public boolean shouldCallDirectly(Object target) { boolean result = depth == -1 ? super.shouldCallDirectly(target) : true; result &= shouldBeDirect(target); if (result) { ++depth; } return result; }
@Override public boolean checkForChange(DirectCallPolicy previousPolicy) { // first setup if (previousPolicy == NOP) { return true; } // we are inside full stack direct call => do not change anything if (previousPolicy instanceof FullStackDirectCallPolicy && ((FullStackDirectCallPolicy) previousPolicy).checkWeAreDeepInsideStack()) { return false; } // unexpected, bad setup throw new DirectCallException("Direct call policy is already set to " + previousPolicy); }