Class ShadowWrangler
- All Implemented Interfaces:
ClassHandler
@AutoService(ClassHandler.class) @Priority(-2147483648) public class ShadowWrangler extends Object implements ClassHandler
ShadowMap.
ShadowWrangler has no specific knowledge of Android SDK levels or other peculiarities of the affected classes
and shadows.
To apply additional rules about which shadow classes and methods are considered matches, pass in a
ShadowMatcher.
ShadowWrangler is Robolectric's default ClassHandler implementation. To inject your own,
create a subclass and annotate it with AutoService(ClassHandler).-
Nested Class Summary
Nested classes/interfaces inherited from interface org.robolectric.internal.bytecode.ClassHandler
ClassHandler.Plan -
Field Summary
Fields Modifier and Type Field Description static MethodCALL_REAL_CODEstatic ClassHandler.PlanCALL_REAL_CODE_PLANstatic MethodHandleDO_NOTHINGstatic Function<Object,Object>DO_NOTHING_HANDLERstatic MethodDO_NOTHING_METHODstatic ClassHandler.PlanDO_NOTHING_PLAN -
Constructor Summary
Constructors Constructor Description ShadowWrangler(ShadowMap shadowMap, ShadowMatcher shadowMatcher, Interceptors interceptors) -
Method Summary
Modifier and Type Method Description voidclassInitializing(Class clazz)Called by Robolectric when an instrumented class is first loaded into a sandbox and is ready to be statically initialized.MethodHandlefindShadowMethodHandle(Class<?> definingClass, String name, MethodType methodType, boolean isStatic)Called by Robolectric when an instrumented method is invoked.MethodHandlegetShadowCreator(Class<?> theClass)Called by Robolectric to determine how to create and initialize a shadow object when a new instance of an instrumented class has been instantiated.Objectinitializing(Object instance)Called by Robolectric when a new instance of an instrumented class has been created and is ready to be initialized (but only on JVMs which don't support theinvokedynamicinstruction).Objectintercept(String signature, Object instance, Object[] params, Class theClass)Called by Robolectric when an intercepted method is invoked.static Class<?>loadClass(String paramType, ClassLoader classLoader)ClassHandler.PlanmethodInvoked(String signature, boolean isStatic, Class<?> theClass)Called by Robolectric when an instrumented method is invoked.protected MethodpickShadowMethod(Class<?> definingClass, String name, Class<?>[] paramTypes)<T extends Throwable>
TstripStackTrace(T throwable)Removes Robolectric noise from stack traces.
-
Field Details
-
Constructor Details
-
Method Details
-
loadClass
-
classInitializing
Description copied from interface:ClassHandlerCalled by Robolectric when an instrumented class is first loaded into a sandbox and is ready to be statically initialized.This happens *in place of* any static initialization that may be performed by the class being loaded. The class will have a method named
__staticInitializer__which may be invoked to perform its normal initialization from<clinit>.- Specified by:
classInitializingin interfaceClassHandler- Parameters:
clazz- the class being loaded
-
initializing
Description copied from interface:ClassHandlerCalled by Robolectric when a new instance of an instrumented class has been created and is ready to be initialized (but only on JVMs which don't support theinvokedynamicinstruction).This happens before constructor code executes on the new instance.
Implementations may return an object which will be associated with the new instance and passed along on future calls to
ClassHandler.methodInvoked(String, boolean, Class).- Specified by:
initializingin interfaceClassHandler- Parameters:
instance- the newly-created instance- Returns:
- a data value to be associated with the new instance
- See Also:
for newer JVMs
-
methodInvoked
Description copied from interface:ClassHandlerCalled by Robolectric when an instrumented method is invoked.Implementations should return an
ClassHandler.Plan, which will be invoked with details about the current instance and parameters.Implementations may also return null, in which case the method's original code will be executed.
- Specified by:
methodInvokedin interfaceClassHandler- Parameters:
signature- the JVM internal-format signature of the method being invoked (e.g.android/view/View/measure(II)V)isStatic- true if the method is statictheClass- the class on which the method is declared- Returns:
- an execution plan, or null if the original method's code should be executed
- See Also:
for newer JVMs
-
findShadowMethodHandle
public MethodHandle findShadowMethodHandle(Class<?> definingClass, String name, MethodType methodType, boolean isStatic) throws IllegalAccessExceptionDescription copied from interface:ClassHandlerCalled by Robolectric when an instrumented method is invoked.Implementations should return an
MethodHandle, which will be invoked with details about the current instance and parameters.Implementations may also return null, in which case the method's original code will be executed.
- Specified by:
findShadowMethodHandlein interfaceClassHandler- Parameters:
definingClass- the class on which the method is declaredname- the name of the methodmethodType- the method typeisStatic- true if the method is static- Returns:
- a method handle to invoke, or null if the original method's code should be executed
- Throws:
IllegalAccessException- See Also:
for older JVMs,for invalidating the returned
-
pickShadowMethod
-
intercept
public Object intercept(String signature, Object instance, Object[] params, Class theClass) throws ThrowableDescription copied from interface:ClassHandlerCalled by Robolectric when an intercepted method is invoked.Unlike instrumented methods, calls to intercepted methods are modified in place by Robolectric in the calling code. This is useful when the method about to be invoked doesn't exist in the current JVM (e.g. because of Android differences).
- Specified by:
interceptin interfaceClassHandler- Parameters:
signature- the JVM internal-format signature of the method being invoked (e.g.android/view/View/measure(II)V)instance- the instance on which the method would have been invokedparams- the parameters to the methodtheClass- the class on which the method is declared- Returns:
- the value to be returned
- Throws:
Throwable- if anything bad happens
-
stripStackTrace
Description copied from interface:ClassHandlerRemoves Robolectric noise from stack traces.- Specified by:
stripStackTracein interfaceClassHandler- Type Parameters:
T- the type of exception- Parameters:
throwable- the exception to be stripped- Returns:
- the stripped stack trace
-
getShadowCreator
Description copied from interface:ClassHandlerCalled by Robolectric to determine how to create and initialize a shadow object when a new instance of an instrumented class has been instantiated. (but only on JVMs which support theinvokedynamicinstruction).The returned
MethodHandlewill be invoked after the new object has been allocated but before its constructor code is executed.Note that this is not directly analogous to
ClassHandler.initializing(Object); the return value from this method will be cached and used again for other instantiations of instances of the same class.- Specified by:
getShadowCreatorin interfaceClassHandler- Parameters:
theClass- the instrumented class- Returns:
- a data value to be associated with the new instance
- See Also:
for older JVMs,for invalidating the returned
-