public final class ReflectionTestUtils extends Object
| Modifier and Type | Field and Description |
|---|---|
static String |
PERMISSION_MSG |
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
getClassByName(String className)
Performs Class.forName, but only throws a RuntimeException so no exception needs to be handled.
|
static Object |
getFieldValue(Field field,
Object instance)
Get the value of a field w/o throwing an Exception that needs to be caught.
|
static <T> T |
getInstanceFieldValue(Object instance,
String fieldName,
Class<T> fieldType)
Get the value of an instance Field.
|
static <T> T |
getStaticFieldValue(Class<?> clazz,
String fieldName,
Class<T> fieldType)
Get the value of a static Field.
|
static Class<?>[] |
getTypes(Object... args)
Get the types of a list of objects for use as argument types.
|
static Field |
getWritableField(Class<?> clazz,
String fieldName)
Get a Field (static or instance), make it accessible and only throw a RuntimeException if there is a problem.
|
static Object |
invokeInstanceMethod(Object instance,
String name,
Object[] args,
Class<?>[] types)
Invokes the named instance method on the instance via reflection, bypassing visibility.
|
static Object |
invokeMethod(Method method,
Object instance,
Object[] args)
Invoke the Method, static or instance, with the passed arguments.
|
static Object |
invokeStaticMethod(Class<?> clazz,
String name,
Object[] args,
Class<?>[] types)
Invokes the named static method via reflection, bypassing visibility.
|
static Object |
setFieldValue(Field field,
Object instance,
Object value)
Get the value of a field w/o throwing an Exception that needs to be caught.
|
static <T> T |
setInstanceFieldValue(Object instance,
String fieldName,
T fieldValue,
Class<T> fieldType)
Set the fieldValue of an instance Field.
|
static <T> T |
setStaticFieldValue(Class<?> clazz,
String fieldName,
T value)
Sets the value of a static Field.
|
static String |
stringMethod(Object instance,
String name,
Object... args)
Invoke a String returning instance method with an array of arguments, all of which must
be non-null.
|
static String |
stringMethod(Object instance,
String name,
Object[] args,
Class<?>[] types)
Invokes the named String returning instance method on the instance via reflection,
bypassing visibility.
|
static String |
stringMethod(Object instance,
String name,
Object arg,
Class<?> type)
Invokes the named String returning, single arg, instance method on the instance via reflection,
bypassing visibility.
|
public static final String PERMISSION_MSG
public static String stringMethod(Object instance, String name, Object[] args, Class<?>[] types)
instance - The object instance to call the method on.name - The name of a String returning method, which must be an instance method.args - Arguments to the methodtypes - The argument types of the method which must match the method, not the args.Exceptionpublic static String stringMethod(Object instance, String name, Object arg, Class<?> type)
instance - The object instance to call the method on.name - The name of a String returning method, which must be an instance method.arg - The single argument to pass to the methodtype - Type of the argument, which allos the argument to be null.public static String stringMethod(Object instance, String name, Object... args)
instance - The object instance to call the method on.name - The name of a String returning method, which must be an instance method.args - Arguments to the method, which must be all non-nullpublic static Object invokeInstanceMethod(Object instance, String name, Object[] args, Class<?>[] types)
instance - The object instance to call the method on.name - The name of a String returning method, which must be an instance method.args - Arguments to the methodtypes - The argument types of the method which must match the method, not the args.public static Object invokeStaticMethod(Class<?> clazz, String name, Object[] args, Class<?>[] types)
clazz - The class to call the method on.name - The name of a String returning method, which must be an instance method.args - Arguments to the method. May be empty or null for zero argument methodstypes - The argument types of the method which must match the method, not the args.
Must be empty for zero arg methods, not null.public static Object invokeMethod(Method method, Object instance, Object[] args)
method - The method to invoke.instance - The instance to invoke the method on, or null if a static method.args - The arguments to pass to the method.public static <T> T getInstanceFieldValue(Object instance, String fieldName, Class<T> fieldType)
instance - The instance to find the field infieldName - The name of a static field in the classfieldType - The type of the fieldpublic static <T> T setInstanceFieldValue(Object instance, String fieldName, T fieldValue, Class<T> fieldType)
instance - The instance to find the field infieldName - The name of a static field in the classfieldValue - The new fieldValue for the fieldfieldType - The type of the field (since fieldValue could be null)public static <T> T getStaticFieldValue(Class<?> clazz, String fieldName, Class<T> fieldType)
class A {
static int classAInt;
}
class B extends A {
static int classBInt;
}
To get the value of classAInt, you must call
getStaticFieldValue(A.class, "classAInt", int.class). If you call the same method on
class B, the field will not be found.clazz - The class to find the field in (not a subclass of it)fieldName - The name of a static field in the classfieldType - The type of the field - Really just a convenience so the caller doesn't have
to cast the result. The value may be null as long as there is a compiler known
type for the argument.public static <T> T setStaticFieldValue(Class<?> clazz, String fieldName, T value)
class A {
static int classAInt;
}
class B extends A {
static int classBInt;
}
To set the value of classAInt, you must call
setStaticFieldValue(A.class, "classAInt", 42). If you call the same method on
class B, the field will not be found.
Since static fields must be uniquely named at the class level, primitives and autoboxing of types is not a problem.
clazz - The class to find the field in (not a subclass of it)fieldName - The name of a static field in the classvalue - The new value for the field.public static Object getFieldValue(Field field, Object instance)
field - The field to retrieve the value of.instance - The object instance the field is a member of, may be null
to access static fields.public static Object setFieldValue(Field field, Object instance, Object value)
field - The field to retrieve the value of.instance - The object instance the field is a member of, may be null
to access static fields.value - The value to assign, which may be null.public static Field getWritableField(Class<?> clazz, String fieldName)
clazz - The class to find the field infieldName - The name of a static field in the classpublic static Class<?>[] getTypes(Object... args)
args - Copyright © 2022. All rights reserved.