Package org.robolectric.util
Class ReflectionHelpers
java.lang.Object
org.robolectric.util.ReflectionHelpers
Collection of helper methods for calling methods and accessing fields reflectively.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Typed parameter used with reflective method calls. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionattemptLoadClass
(ClassLoader classLoader, String fullyQualifiedClassName) Attempt to load a class.static <R> R
callConstructor
(Class<? extends R> clazz, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call the constructor of an object.static <R> R
callInstanceMethod
(Class<?> cl, Object instance, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call an instance method on an object on a specific class.static <R> R
callInstanceMethod
(Object instance, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call an instance method on an object.static <R> R
callStaticMethod
(Class<?> clazz, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call a static method on a class.static <R> R
callStaticMethod
(ClassLoader classLoader, String fullyQualifiedClassName, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Helper method for calling a static method using a class from a custom class loaderstatic <T> T
createDeepProxy
(Class<T> clazz) Create a proxy for the given class which returns other deep proxies from all it's methods.static <T> T
createDelegatingProxy
(Class<T> clazz, Object delegate) Create a proxy for the given class which can delegate method calls to another object.static <T> T
createNullProxy
(Class<T> clazz) Create a proxy for the given class which returns default values for every method call.static <A extends Annotation>
AdefaultsFor
(Class<A> annotation) static Object
defaultValueForType
(String returnType) static <R> R
Reflectively get the value of a field.static <R> R
getStaticField
(Class<?> clazz, String fieldName) Reflectively get the value of a static field.static <R> R
getStaticField
(Field field) Reflectively get the value of a static field.static boolean
Reflectively check if a class has a given field (static or non static).static Class<?>
loadClass
(ClassLoader classLoader, String fullyQualifiedClassName) Load a class.static <T> T
newInstance
(Class<T> cl) Create a new instance of a classstatic void
Reflectively set the value of a field.static void
Reflectively set the value of a field.static void
setStaticField
(Class<?> clazz, String fieldName, Object fieldNewValue) Reflectively set the value of a static field.static void
setStaticField
(Field field, Object fieldNewValue) Reflectively set the value of a static field.
-
Constructor Details
-
ReflectionHelpers
public ReflectionHelpers()
-
-
Method Details
-
createNullProxy
Create a proxy for the given class which returns default values for every method call.0 will be returned for any primitive return types, otherwise null will be returned.
- Parameters:
clazz
- the class to provide a proxy instance of.- Returns:
- a new "Null Proxy" instance of the given class.
-
createDeepProxy
Create a proxy for the given class which returns other deep proxies from all it's methods.The returned object will be an instance of the given class, but all methods will return either the "default" value for primitives, or another deep proxy for non-primitive types.
This should be used rarely, for cases where we need to create deep proxies in order not to crash. The inner proxies are impossible to configure, so there is no way to create meaningful behavior from a deep proxy. It serves mainly to prevent Null Pointer Exceptions.
- Parameters:
clazz
- the class to provide a proxy instance of.- Returns:
- a new "Deep Proxy" instance of the given class.
-
createDelegatingProxy
Create a proxy for the given class which can delegate method calls to another object.If the delegate has no methods whose signature matches, a null (or 0 for primitive types) return value will be returned.
- Parameters:
clazz
- the class to provide a proxy instance of.delegate
- the object to delegate matching method calls to. A 'matching method' must have exactlu the same method name and parameter class names as the desired method. The @ClassName annotation can be applied to provide a custom class name.- Returns:
- a new "Delegating Proxy" instance of the given class.
-
defaultsFor
-
getField
Reflectively get the value of a field.- Type Parameters:
R
- The return type.- Parameters:
object
- Target object.fieldName
- The field name.- Returns:
- Value of the field on the object.
-
setField
Reflectively set the value of a field.- Parameters:
object
- Target object.fieldName
- The field name.fieldNewValue
- New value.
-
setField
Reflectively set the value of a field.- Parameters:
type
- Target type.object
- Target object.fieldName
- The field name.fieldNewValue
- New value.
-
hasField
Reflectively check if a class has a given field (static or non static).- Parameters:
clazz
- Target class.fieldName
- The field name.- Returns:
- boolean to indicate whether the field exists or not in clazz.
-
getStaticField
Reflectively get the value of a static field.- Type Parameters:
R
- The return type.- Parameters:
field
- Field object.- Returns:
- Value of the field.
-
getStaticField
Reflectively get the value of a static field.- Type Parameters:
R
- The return type.- Parameters:
clazz
- Target class.fieldName
- The field name.- Returns:
- Value of the field.
-
setStaticField
Reflectively set the value of a static field.- Parameters:
field
- Field object.fieldNewValue
- The new value.
-
setStaticField
Reflectively set the value of a static field.- Parameters:
clazz
- Target class.fieldName
- The field name.fieldNewValue
- The new value.
-
callInstanceMethod
public static <R> R callInstanceMethod(Object instance, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call an instance method on an object.- Type Parameters:
R
- The return type.- Parameters:
instance
- Target object.methodName
- The method name to call.classParameters
- Array of parameter types and values.- Returns:
- The return value of the method.
-
callInstanceMethod
public static <R> R callInstanceMethod(Class<?> cl, Object instance, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call an instance method on an object on a specific class.- Type Parameters:
R
- The return type.- Parameters:
cl
- The class.instance
- Target object.methodName
- The method name to call.classParameters
- Array of parameter types and values.- Returns:
- The return value of the method.
-
callStaticMethod
public static <R> R callStaticMethod(ClassLoader classLoader, String fullyQualifiedClassName, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Helper method for calling a static method using a class from a custom class loader- Type Parameters:
R
- Return type of the method- Parameters:
classLoader
- The ClassLoader used to load classfullyQualifiedClassName
- The full qualified class name with package name of the ClassLoader will loadmethodName
- The method name will be calledclassParameters
- The input parameters will be used for method calling- Returns:
- Return the value of the method
-
callStaticMethod
public static <R> R callStaticMethod(Class<?> clazz, String methodName, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call a static method on a class.- Type Parameters:
R
- The return type.- Parameters:
clazz
- Target class.methodName
- The method name to call.classParameters
- Array of parameter types and values.- Returns:
- The return value of the method.
-
loadClass
Load a class.- Parameters:
classLoader
- The class loader.fullyQualifiedClassName
- The fully qualified class name.- Returns:
- The class object.
-
attemptLoadClass
public static Optional<Class<?>> attemptLoadClass(ClassLoader classLoader, String fullyQualifiedClassName) Attempt to load a class.- Parameters:
classLoader
- The class loader.fullyQualifiedClassName
- The fully qualified class name.- Returns:
- The class object, or null if class is not found.
-
newInstance
Create a new instance of a class- Type Parameters:
T
- The class type.- Parameters:
cl
- The class object.- Returns:
- New class instance.
-
callConstructor
public static <R> R callConstructor(Class<? extends R> clazz, ReflectionHelpers.ClassParameter<?>... classParameters) Reflectively call the constructor of an object.- Type Parameters:
R
- The return type.- Parameters:
clazz
- Target class.classParameters
- Array of parameter types and values.- Returns:
- The return value of the method.
-
defaultValueForType
-