private static RobolectricClassLoader getDefaultLoader() { if (defaultLoader == null) { defaultLoader = new RobolectricClassLoader(ShadowWrangler.getInstance()); } return defaultLoader; }
public String generateMethodBody(CtMethod ctMethod, boolean wasNative, boolean wasAbstract, CtClass returnCtClass, Type returnType, boolean aStatic, boolean shouldGenerateCallToSuper) throws NotFoundException { String methodBody; if (wasAbstract) { methodBody = returnType.isVoid() ? "" : "return " + returnType.defaultReturnString() + ";"; } else { methodBody = generateMethodBody(ctMethod, returnCtClass, returnType, aStatic, shouldGenerateCallToSuper); } if (wasNative) { methodBody += returnType.isVoid() ? "" : "return " + returnType.defaultReturnString() + ";"; } return methodBody; }
@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); }
public static <T> T directlyOnFullStack(FullStackDirectCallPolicy.Builder<T> builder) { FullStackDirectCallPolicy policy = builder.create(); setupDirectCallPolicy(policy); return builder.getTarget(); }
/** * Invoke this utility method in tests to reveal which Android api classes and methods are being invoked * for which there are no shadows or shadow methods. This helps expose which methods are being invoked * either by a third party library or application code which need new shadow methods to be written. Generates * output for the current test only. */ public static void logMissingInvokedShadowMethods() { ShadowWrangler.getInstance().logMissingInvokedShadowMethods(); }
public static <T> T directlyOn(T shadowedObject) { setupDirectCallPolicy(new DirectCallPolicy.OneShotDirectCallPolicy(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 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; }
private void injectRealObjectOn(Object shadow, Class<?> shadowClass, Object instance) { MetaShadow metaShadow = getMetaShadow(shadowClass); for (Field realObjectField : metaShadow.realObjectFields) { writeField(shadow, instance, realObjectField); } }
public static <T> T directlyOnFullStack(T shadowedObject) { setupDirectCallPolicy(FullStackDirectCallPolicy.withTarget(shadowedObject)); return shadowedObject; }
private Class<?> findDeclaredShadowClassForMethod(Class<?> originalClass, String methodName, Class<?>[] paramClasses) { Class<?> declaringClass = findDeclaringClassForMethod(methodName, paramClasses, originalClass); return findShadowClass(declaringClass); }
private Class<?>[] getParamClasses() { Class<?>[] paramClasses = new Class<?>[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) { paramClasses[i] = loadClass(paramTypes[i], classLoader); } return paramClasses; }
@Override public boolean shouldCallDirectly(Object target) { if (expectedInstance == null) { return false; } if (expectedInstance != target) { Object expected = expectedInstance; expectedInstance = null; throw new DirectCallException("expected to perform direct call on <" + expected + "> but got <" + target + ">"); } return true; } }
@Override public boolean shouldCallDirectly(Object target) { boolean result = super.shouldCallDirectly(target); expectedInstance = null; return result; }
public static ShadowWrangler getInstance() { if (singleton == null) { singleton = new ShadowWrangler(); } return singleton; }
@Override public void start(ClassPool classPool) throws NotFoundException, CannotCompileException { injectClassHandlerToInstrumentedClasses(classPool); }
private static RobolectricClassLoader getDefaultLoader() { if (defaultLoader == null) { defaultLoader = new RobolectricClassLoader(ShadowWrangler.getInstance()); } return defaultLoader; }
@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); } }
private Class<?> findShadowClass(Class<?> originalClass) { String declaredShadowClassName = getShadowClassName(originalClass); if (declaredShadowClassName == null) { return null; } return loadClass(declaredShadowClassName, classLoader); }