public class ReflectionHelpers
extends java.lang.Object
Collection of helper methods for calling methods and accessing fields reflectively.
Modifier and Type | Class and Description |
---|---|
static class |
ReflectionHelpers.ClassParameter<V>
Typed parameter used with reflective method calls.
|
static class |
ReflectionHelpers.StringParameter<V>
String parameter used with reflective method calls.
|
Modifier and Type | Field and Description |
---|---|
static java.util.Map<java.lang.String,java.lang.Object> |
PRIMITIVE_RETURN_VALUES |
Constructor and Description |
---|
ReflectionHelpers() |
Modifier and Type | Method and Description |
---|---|
static <R> R |
callConstructor(java.lang.Class<? extends R> clazz,
ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call the constructor of an object.
|
static <R> R |
callInstanceMethod(java.lang.Class<?> cl,
java.lang.Object instance,
java.lang.String methodName,
ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call an instance method on an object on a specific class.
|
static <R> R |
callInstanceMethod(java.lang.Object instance,
java.lang.String methodName,
ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call an instance method on an object.
|
static <R> R |
callStaticMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call a static method on a class.
|
static <T> T |
createDeepProxy(java.lang.Class<T> clazz)
Create a proxy for the given class which returns other deep proxies from all it’s methods.
|
static <T> T |
createDelegatingProxy(java.lang.Class<T> clazz,
java.lang.Object delegate) |
static <T> T |
createNullProxy(java.lang.Class<T> clazz) |
static <A extends java.lang.annotation.Annotation> |
defaultsFor(java.lang.Class<A> annotation) |
static java.lang.Object |
defaultValueForType(java.lang.String returnType) |
static <R> R |
getField(java.lang.Object object,
java.lang.String fieldName)
Reflectively get the value of a field.
|
static <R> R |
getStaticField(java.lang.Class<?> clazz,
java.lang.String fieldName)
Reflectively get the value of a static field.
|
static <R> R |
getStaticField(java.lang.reflect.Field field)
Reflectively get the value of a static field.
|
static java.lang.Class<?> |
loadClass(java.lang.ClassLoader classLoader,
java.lang.String fullyQualifiedClassName)
Load a class.
|
static <T> T |
newInstance(java.lang.Class<T> cl)
Create a new instance of a class
|
static void |
setField(java.lang.Class<?> type,
java.lang.Object object,
java.lang.String fieldName,
java.lang.Object fieldNewValue)
Reflectively set the value of a field.
|
static void |
setField(java.lang.Object object,
java.lang.String fieldName,
java.lang.Object fieldNewValue)
Reflectively set the value of a field.
|
static void |
setStaticField(java.lang.Class<?> clazz,
java.lang.String fieldName,
java.lang.Object fieldNewValue)
Reflectively set the value of a static field.
|
static void |
setStaticField(java.lang.reflect.Field field,
java.lang.Object fieldNewValue)
Reflectively set the value of a static field.
|
public static final java.util.Map<java.lang.String,java.lang.Object> PRIMITIVE_RETURN_VALUES
public static <T> T createNullProxy(java.lang.Class<T> clazz)
public static <T> T createDeepProxy(java.lang.Class<T> clazz)
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.
clazz
- the class to provide a proxy instance of.public static <T> T createDelegatingProxy(java.lang.Class<T> clazz, java.lang.Object delegate)
public static <A extends java.lang.annotation.Annotation> A defaultsFor(java.lang.Class<A> annotation)
public static <R> R getField(java.lang.Object object, java.lang.String fieldName)
Reflectively get the value of a field.
R
- The return type.object
- Target object.fieldName
- The field name.public static void setField(java.lang.Object object, java.lang.String fieldName, java.lang.Object fieldNewValue)
Reflectively set the value of a field.
object
- Target object.fieldName
- The field name.fieldNewValue
- New value.public static void setField(java.lang.Class<?> type, java.lang.Object object, java.lang.String fieldName, java.lang.Object fieldNewValue)
Reflectively set the value of a field.
type
- Target type.object
- Target object.fieldName
- The field name.fieldNewValue
- New value.public static <R> R getStaticField(java.lang.reflect.Field field)
Reflectively get the value of a static field.
R
- The return type.field
- Field object.public static <R> R getStaticField(java.lang.Class<?> clazz, java.lang.String fieldName)
Reflectively get the value of a static field.
R
- The return type.clazz
- Target class.fieldName
- The field name.public static void setStaticField(java.lang.reflect.Field field, java.lang.Object fieldNewValue)
Reflectively set the value of a static field.
field
- Field object.fieldNewValue
- The new value.public static void setStaticField(java.lang.Class<?> clazz, java.lang.String fieldName, java.lang.Object fieldNewValue)
Reflectively set the value of a static field.
clazz
- Target class.fieldName
- The field name.fieldNewValue
- The new value.public static <R> R callInstanceMethod(java.lang.Object instance, java.lang.String methodName, ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call an instance method on an object.
R
- The return type.instance
- Target object.methodName
- The method name to call.classParameters
- Array of parameter types and values.public static <R> R callInstanceMethod(java.lang.Class<?> cl, java.lang.Object instance, java.lang.String methodName, ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call an instance method on an object on a specific class.
R
- The return type.cl
- The class.instance
- Target object.methodName
- The method name to call.classParameters
- Array of parameter types and values.public static <R> R callStaticMethod(java.lang.Class<?> clazz, java.lang.String methodName, ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call a static method on a class.
R
- The return type.clazz
- Target class.methodName
- The method name to call.classParameters
- Array of parameter types and values.public static java.lang.Class<?> loadClass(java.lang.ClassLoader classLoader, java.lang.String fullyQualifiedClassName)
Load a class.
classLoader
- The class loader.fullyQualifiedClassName
- The fully qualified class name.public static <T> T newInstance(java.lang.Class<T> cl)
Create a new instance of a class
T
- The class type.cl
- The class object.public static <R> R callConstructor(java.lang.Class<? extends R> clazz, ReflectionHelpers.ClassParameter<?>... classParameters)
Reflectively call the constructor of an object.
R
- The return type.clazz
- Target class.classParameters
- Array of parameter types and values.public static java.lang.Object defaultValueForType(java.lang.String returnType)