public class ReflectionUtils
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.lang.Object[] |
EMPTY_ARGUMENTS
Predeclared empty object array (new Object[0]).
|
static java.lang.Class<?>[] |
EMPTY_TYPES
Predeclared empty class array (new Class>[0]).
|
static java.util.Set<java.lang.Class<?>> |
WRAPPERS
Set containing primitive wrapper classes.
|
| Constructor and Description |
|---|
ReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> java.lang.Class<T> |
classForName(java.lang.String className)
An alternative to
Class.forName(String) which wraps checked ClassNotFoundException exception into
unchecked ReflectionException |
static java.lang.reflect.Field |
findField(java.lang.Class<?> clazz,
java.lang.String fieldName)
Returns declared field of specified class.
|
static java.lang.reflect.Field |
findFieldRecursively(java.lang.Class<?> clazz,
org.srplib.support.Path path)
Searches field specified as
Path. |
static java.lang.reflect.Field |
findFieldRecursively(java.lang.Class<?> clazz,
java.lang.String fieldName)
Searches recursively and returns declared field with specified name.
|
static java.lang.reflect.Method |
findMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
Searches and returns specified method in specified class.
|
static java.lang.reflect.Method |
findMethodRecursively(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
Searches and returns specified method recursively.
|
static java.lang.reflect.Field |
getField(java.lang.Class<?> clazz,
java.lang.String fieldName)
Returns declared field of specified class.
|
static java.lang.reflect.Field |
getFieldRecursively(java.lang.Class<?> clazz,
org.srplib.support.Path path)
Searches recursively and returns declared field with specified name.
|
static java.lang.reflect.Field |
getFieldRecursively(java.lang.Class<?> clazz,
java.lang.String fieldName)
Searches recursively and returns declared field with specified name.
|
static java.util.List<java.lang.reflect.Field> |
getFields(java.lang.Class<?> clazz)
Returns list of declared fields of specified class.
|
static java.util.List<java.lang.reflect.Field> |
getFieldsRecursively(java.lang.Class<?> clazz)
Returns list of declared fields of specified class including fields of superclasses.
|
static <T> java.lang.Class<T> |
getFieldType(java.lang.Class<?> clazz,
java.lang.String fieldName)
Returns field type.
|
static <T> T |
getFieldValue(java.lang.Object target,
java.lang.reflect.Field field)
Set value of specified field of specified object.
|
static <T> T |
getFieldValue(java.lang.Object target,
org.srplib.support.Path path)
Returns value of nested field specified as path.
|
static <T> T |
getFieldValue(java.lang.Object target,
java.lang.String fieldName)
Returns value of specified property of specified object.
|
static java.lang.Object |
getInitValue(java.lang.Class<?> type)
Returns init value for specified class (including classes representing primitives).
|
static java.lang.reflect.Method |
getMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
Searches and returns specified method in specified class.
|
static java.lang.reflect.Method |
getMethodRecursively(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
Searches and returns specified method recursively.
|
static <T> java.lang.Class<T> |
getTypeParameter(java.lang.Class<?> clazz)
Returns first generic type parameter of specified class.
|
static <T> java.lang.Class<T> |
getTypeParameter(java.lang.Class<?> clazz,
java.lang.String message,
java.lang.Object... parameters)
Returns first generic type parameter of specified class with specific error message.
|
static java.util.List<java.lang.Class<?>> |
getTypeParameters(java.lang.Class<?> clazz)
Returns first generic type parameter of specified class.
|
static boolean |
hasConstructor(java.lang.Class<?> clazz,
java.lang.Class<?>[] parameterTypes)
Checks for constructor existence.
|
static <T> T |
invokeMethod(java.lang.reflect.Method method,
java.lang.Object object,
java.lang.Object... arguments)
Deprecated.
use invokeMethod(Object, Method, Object...)
|
static <T> T |
invokeMethod(java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object... arguments)
Invokes specified method of specified target using reflection.
|
static boolean |
isArray(java.lang.Class<?> clazz)
Deprecated.
use Types class
|
static boolean |
isCollection(java.lang.Class<?> clazz)
Tests if specified class is a collection.
|
static boolean |
isComplexType(java.lang.Class<?> clazz)
Tests if specified class represents "complex" type.
|
static boolean |
isMap(java.lang.Class<?> clazz)
Tests if specified class is a map.
|
static boolean |
isPrimitiveArray(java.lang.Class<?> clazz)
Deprecated.
use Types class
|
static boolean |
isPrimitiveWrapper(java.lang.Class<?> clazz)
Deprecated.
use Types class
|
static boolean |
isPrimitiveWrapperArray(java.lang.Class<?> clazz)
Deprecated.
use Types class
|
static boolean |
isSimpleType(java.lang.Class<?> clazz)
Tests if specified class represents "simple" type.
|
static boolean |
isSyntheticName(java.lang.String name)
Tests if specified name is a synthetic name.
|
static <T> T |
newInstance(java.lang.Class<T> clazz)
Creates instance of specified class and wraps checked exceptions into unchecked ones.
|
static <T> T |
newInstance(java.lang.Class<T> clazz,
java.lang.Class[] parameters,
java.lang.Object[] arguments)
Creates instance of specified class and wraps checked exceptions into unchecked ones.
|
static <T> T |
newInstance(java.lang.String className)
Creates instance of specified class and wraps checked exceptions into unchecked ones.
|
static void |
setFieldValue(java.lang.Object target,
java.lang.reflect.Field field,
java.lang.Object value)
Set value of specified field of specified object.
|
static void |
setFieldValue(java.lang.Object target,
org.srplib.support.Path path,
java.lang.Object value)
Set value of specified field of specified object.
|
static void |
setFieldValue(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object value)
Set value of specified field of specified object.
|
public static final java.lang.Object[] EMPTY_ARGUMENTS
public static final java.lang.Class<?>[] EMPTY_TYPES
public static final java.util.Set<java.lang.Class<?>> WRAPPERS
public static java.lang.Object getInitValue(java.lang.Class<?> type)
type - Class classpublic static java.util.List<java.lang.Class<?>> getTypeParameters(java.lang.Class<?> clazz)
clazz - Class source classnull if class has no type parameters.public static <T> java.lang.Class<T> getTypeParameter(java.lang.Class<?> clazz)
clazz - Class source classnull if class has no type parameters.public static <T> java.lang.Class<T> getTypeParameter(java.lang.Class<?> clazz,
java.lang.String message,
java.lang.Object... parameters)
clazz - Class source classmessage - String error message pattern, Message will be used to construct exception text.parameters - Object vararg. parameter for message pattern.null if class has no type parameters.public static java.lang.reflect.Method findMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
clazz - Class starting class to search method inmethodName - String method name.parameters - an array of parameter typesnull otherwisepublic static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
clazz - Class starting class to search method inmethodName - String method name.parameters - an array of parameter typesnull otherwiseReflectionException - if no method foundpublic static java.lang.reflect.Method findMethodRecursively(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
clazz - Class starting class to search method in.methodName - String method name (non-null).parameters - an array of parameter typesnull otherwise. Returns null if clazz is null.public static java.lang.reflect.Method getMethodRecursively(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... parameters)
clazz - Class starting class to search method inmethodName - String method name.parameters - an array of parameter typesReflectionException - if no method found.public static java.lang.reflect.Field findField(java.lang.Class<?> clazz,
java.lang.String fieldName)
clazz - Class class to introspect.null if no such field in specified class.public static java.lang.reflect.Field getField(java.lang.Class<?> clazz,
java.lang.String fieldName)
clazz - Class class to introspect.public static java.lang.reflect.Field findFieldRecursively(java.lang.Class<?> clazz,
java.lang.String fieldName)
clazz - Class class to start search fromfieldName - String field namenull if field was not found in class or superclassespublic static java.lang.reflect.Field findFieldRecursively(java.lang.Class<?> clazz,
org.srplib.support.Path path)
Path.
TODO: finder should not throw exceptionclazz - Class classjava.lang.IllegalArgumentException - if class or path is nullReflectionException - if intermediate field is not foundpublic static java.lang.reflect.Field getFieldRecursively(java.lang.Class<?> clazz,
org.srplib.support.Path path)
clazz - Class class to start search frompath - Path field pathnull if field was not found in class or superclassesReflectionException - if no such field foundpublic static java.lang.reflect.Field getFieldRecursively(java.lang.Class<?> clazz,
java.lang.String fieldName)
clazz - Class class to start search fromfieldName - String field namenull if field was not found in class or superclassesReflectionException - if no such field foundpublic static java.util.List<java.lang.reflect.Field> getFields(java.lang.Class<?> clazz)
clazz - Class class to introspect.public static java.util.List<java.lang.reflect.Field> getFieldsRecursively(java.lang.Class<?> clazz)
clazz - Class class to introspect.public static void setFieldValue(java.lang.Object target,
java.lang.reflect.Field field,
java.lang.Object value)
Method wraps all checked exceptions into unchecked exceptions.
target - Object the object the underlying method is invoked fromfield - Field field to set value to.value - Object value to set to fieldReflectionException - if can't set field value.public static void setFieldValue(java.lang.Object target,
java.lang.String fieldName,
java.lang.Object value)
Method wraps all checked exceptions into unchecked exceptions.
target - Object target objectfieldName - String field name to set value to.value - Object value to set to a fieldReflectionException - if can't set field value.public static void setFieldValue(java.lang.Object target,
org.srplib.support.Path path,
java.lang.Object value)
target - Object target objectpath - Path field pathvalue - Object value to set to fieldjava.lang.IllegalArgumentException - if target of path are nullReflectionException - in case of reflection errorpublic static <T> T getFieldValue(java.lang.Object target,
java.lang.reflect.Field field)
Method wraps all checked exceptions into unchecked exceptions.
target - Object target objectfield - Field field to set value to.ReflectionException - if can't get field value.public static <T> T getFieldValue(java.lang.Object target,
java.lang.String fieldName)
Method wraps all checked exceptions into unchecked exceptions.
target - Object target objectfieldName - String field nameReflectionException - if can't get field value.public static <T> T getFieldValue(java.lang.Object target,
org.srplib.support.Path path)
target - Object target object.path - Path field pathjava.lang.IllegalArgumentException - if target or path are nullReflectionException - if case of reflection errorpublic static <T> java.lang.Class<T> getFieldType(java.lang.Class<?> clazz,
java.lang.String fieldName)
clazz - Class a class where field is declaredfieldName - String field namejava.lang.IllegalStateException - if no such field in specified class@Deprecated
public static <T> T invokeMethod(java.lang.reflect.Method method,
java.lang.Object object,
java.lang.Object... arguments)
Method wraps all checked exceptions into unchecked exceptions.
method - Method to invokeobject - Object the object the underlying method is invoked fromarguments - vararg array of method arguments.public static <T> T invokeMethod(java.lang.Object target,
java.lang.reflect.Method method,
java.lang.Object... arguments)
Method wraps all checked exceptions into unchecked exceptions.
method - Method to invoketarget - Object the target the underlying method is invoked fromarguments - vararg array of method arguments.public static <T> T newInstance(java.lang.Class<T> clazz,
java.lang.Class[] parameters,
java.lang.Object[] arguments)
Converts checked exceptions to unchecked
clazz - Class a class to create instanceparameters - Class[] array of constructor parameters (parameter types).arguments - Object[] an array of constructor arguments.public static <T> T newInstance(java.lang.Class<T> clazz)
Converts checked exceptions to unchecked
clazz - Class a class to create instancepublic static <T> T newInstance(java.lang.String className)
Converts checked exceptions to unchecked
className - String class name of a class to create instancepublic static boolean hasConstructor(java.lang.Class<?> clazz,
java.lang.Class<?>[] parameterTypes)
public static <T> java.lang.Class<T> classForName(java.lang.String className)
Class.forName(String) which wraps checked ClassNotFoundException exception into
unchecked ReflectionExceptionclassName - String full class namejava.lang.IllegalArgumentException - if class name is null.ReflectionException - if reflection API throws ClassNotFoundExceptionpublic static boolean isArray(java.lang.Class<?> clazz)
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.public static boolean isCollection(java.lang.Class<?> clazz)
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.public static boolean isMap(java.lang.Class<?> clazz)
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.public static boolean isSimpleType(java.lang.Class<?> clazz)
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.public static boolean isComplexType(java.lang.Class<?> clazz)
Complex type is a type which is not simple (see isSimpleType(Class)).
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.public static boolean isSyntheticName(java.lang.String name)
name - String identifier namepublic static boolean isPrimitiveWrapper(java.lang.Class<?> clazz)
Primitive wrappers are listed in WRAPPERS set.
clazz - Class a class to testpublic static boolean isPrimitiveArray(java.lang.Class<?> clazz)
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.public static boolean isPrimitiveWrapperArray(java.lang.Class<?> clazz)
clazz - Class a class to testjava.lang.IllegalArgumentException - if the specified Class parameter is null.