ShadowWrangler
public interface ClassHandler
Modifier and Type | Interface | Description |
---|---|---|
static interface |
ClassHandler.Plan |
An execution plan, which can be invoked to satisfy a requested method call.
|
Modifier and Type | Method | Description |
---|---|---|
void |
classInitializing(Class clazz) |
Called by Robolectric when an instrumented class is first loaded into a sandbox and is ready to
be statically initialized.
|
MethodHandle |
findShadowMethodHandle(Class<?> theClass,
String name,
MethodType methodType,
boolean isStatic) |
Called by Robolectric when an instrumented method is invoked.
|
MethodHandle |
getShadowCreator(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.
|
Object |
initializing(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 the
invokedynamic
instruction). |
Object |
intercept(String signature,
Object instance,
Object[] params,
Class<?> theClass) |
Called by Robolectric when an intercepted method is invoked.
|
ClassHandler.Plan |
methodInvoked(String signature,
boolean isStatic,
Class<?> theClass) |
Called by Robolectric when an instrumented method is invoked.
|
<T extends Throwable> |
stripStackTrace(T throwable) |
Removes Robolectric noise from stack traces.
|
void classInitializing(Class clazz)
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>
.
clazz
- the class being loadedObject initializing(Object instance)
invokedynamic
instruction).
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 methodInvoked(String, boolean, Class)
.
instance
- the newly-created instancefor newer JVMs
ClassHandler.Plan methodInvoked(String signature, boolean isStatic, Class<?> theClass)
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.
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 declaredfor newer JVMs
MethodHandle getShadowCreator(Class<?> theClass)
invokedynamic
instruction).
The returned MethodHandle
will be invoked after the new object has been allocated
but before its constructor code is executed.
Note that this is not directly analogous to initializing(Object)
; the return value
from this method will be cached and used again for other instantiations of instances of the
same class.
theClass
- the instrumented classfor older JVMs
,
for invalidating the returned
MethodHandle findShadowMethodHandle(Class<?> theClass, String name, MethodType methodType, boolean isStatic) throws IllegalAccessException
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.
theClass
- the class on which the method is declaredname
- the name of the methodmethodType
- the method typeisStatic
- true if the method is staticIllegalAccessException
for older JVMs
,
for invalidating the returned
Object intercept(String signature, Object instance, Object[] params, Class<?> theClass) throws Throwable
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).
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 declaredThrowable
- if anything bad happens<T extends Throwable> T stripStackTrace(T throwable)
T
- the type of exceptionthrowable
- the exception to be stripped