public class ReflectionUtil
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.Class<?>[] |
EMPTY_CLASS_ARRAY |
private static java.lang.String[] |
EMPTY_STRING_ARRAY |
private static Log |
log |
private static java.lang.Class<?>[] |
primitiveArrays |
private static java.lang.Class<?>[] |
primitives |
Constructor and Description |
---|
ReflectionUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
extractFieldName(java.lang.String setterOrGetter) |
private static java.lang.reflect.Field |
findFieldRecursively(java.lang.Class<?> c,
java.lang.String fieldName) |
static java.lang.reflect.Method |
findGetterForField(java.lang.Class<?> c,
java.lang.String fieldName) |
private static java.lang.reflect.Method |
findGetterForFieldUsingReflection(java.lang.Class<?> c,
java.lang.String fieldName) |
static java.lang.reflect.Method |
findMethod(java.lang.Class<?> type,
java.lang.String methodName) |
static java.lang.reflect.Method |
findMethod(java.lang.Class<?> type,
java.lang.String methodName,
java.lang.Class<?>[] parameters) |
static java.lang.reflect.Method |
findSetterForField(java.lang.Class<?> c,
java.lang.String fieldName) |
static java.util.List<java.lang.reflect.Method> |
getAllMethods(java.lang.Class<?> c,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a set of Methods that contain the given method annotation.
|
static java.util.List<java.lang.reflect.Method> |
getAllMethodsShallow(java.lang.Class<?> c,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Returns a set of Methods that contain the given method annotation.
|
private static void |
getAnnotatedFieldHelper(java.util.List<java.lang.reflect.Field> list,
java.lang.Class<?> c,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) |
static java.util.List<java.lang.reflect.Field> |
getAnnotatedFields(java.lang.Class<?> c,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType) |
static <T extends java.lang.annotation.Annotation> |
getAnnotation(java.lang.Class<?> clazz,
java.lang.Class<T> ann)
Inspects the class passed in for the class level annotation specified.
|
static java.lang.Class<?> |
getClassForName(java.lang.String name,
java.lang.ClassLoader cl) |
static java.lang.reflect.Field |
getField(java.lang.String fieldName,
java.lang.Class<?> objectClass) |
static java.lang.Object |
getValue(java.lang.Object instance,
java.lang.String fieldName)
Retrieves the value of a field of an object instance via reflection
|
private static void |
inspectRecursively(java.lang.Class<?> c,
java.util.List<java.lang.reflect.Method> s,
java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Inspects a class and its superclasses (all the way to
Object for method instances that contain a given
annotation. |
static java.lang.Object |
invokeAccessibly(java.lang.Object instance,
java.lang.reflect.Method method,
java.lang.Object[] parameters)
Invokes a method using reflection, in an accessible manner (by using
AccessibleObject.setAccessible(boolean) |
static boolean |
isAnnotationPresent(java.lang.Class<?> clazz,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Tests whether an annotation is present on a class.
|
private static boolean |
notFound(java.lang.reflect.Method m,
java.util.Collection<java.lang.reflect.Method> s)
Tests whether a method has already been found, i.e., overridden.
|
static java.lang.Class<?>[] |
toClassArray(java.lang.String[] typeList,
java.lang.ClassLoader classLoader) |
static java.lang.String[] |
toStringArray(java.lang.Class<?>[] classes) |
static <T> T |
unwrap(java.lang.Object obj,
java.lang.Class<T> clazz) |
private static final Log log
private static final java.lang.String[] EMPTY_STRING_ARRAY
private static final java.lang.Class<?>[] primitives
private static final java.lang.Class<?>[] primitiveArrays
public static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
public static java.util.List<java.lang.reflect.Method> getAllMethods(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
c
- class to inspectannotationType
- the type of annotation to look forpublic static java.util.List<java.lang.reflect.Method> getAllMethodsShallow(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
c
- class to inspectannotationType
- the type of annotation to look forprivate static void getAnnotatedFieldHelper(java.util.List<java.lang.reflect.Field> list, java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
public static java.util.List<java.lang.reflect.Field> getAnnotatedFields(java.lang.Class<?> c, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
public static java.lang.reflect.Method findMethod(java.lang.Class<?> type, java.lang.String methodName)
public static java.lang.reflect.Method findMethod(java.lang.Class<?> type, java.lang.String methodName, java.lang.Class<?>[] parameters) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
private static void inspectRecursively(java.lang.Class<?> c, java.util.List<java.lang.reflect.Method> s, java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
Object
for method instances that contain a given
annotation. This even identifies private, package and protected methods, not just public ones.c
- s
- annotationType
- private static boolean notFound(java.lang.reflect.Method m, java.util.Collection<java.lang.reflect.Method> s)
m
- method to inspects
- collection of methods foundprivate static java.lang.reflect.Field findFieldRecursively(java.lang.Class<?> c, java.lang.String fieldName)
public static java.lang.Object invokeAccessibly(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object[] parameters)
AccessibleObject.setAccessible(boolean)
instance
- instance on which to execute the methodmethod
- method to executeparameters
- parameterspublic static java.lang.reflect.Method findGetterForField(java.lang.Class<?> c, java.lang.String fieldName)
private static java.lang.reflect.Method findGetterForFieldUsingReflection(java.lang.Class<?> c, java.lang.String fieldName)
public static java.lang.reflect.Method findSetterForField(java.lang.Class<?> c, java.lang.String fieldName)
public static java.lang.String extractFieldName(java.lang.String setterOrGetter)
public static java.lang.Object getValue(java.lang.Object instance, java.lang.String fieldName)
instance
- to inspectfieldName
- name of field to retrievepublic static <T extends java.lang.annotation.Annotation> T getAnnotation(java.lang.Class<?> clazz, java.lang.Class<T> ann)
clazz
- class to inspectann
- annotation to search for. Must be a class-level annotation.public static boolean isAnnotationPresent(java.lang.Class<?> clazz, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
clazz
- class to testannotation
- annotation to look forpublic static java.lang.Class<?>[] toClassArray(java.lang.String[] typeList, java.lang.ClassLoader classLoader) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public static java.lang.Class<?> getClassForName(java.lang.String name, java.lang.ClassLoader cl) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public static java.lang.String[] toStringArray(java.lang.Class<?>[] classes)
public static java.lang.reflect.Field getField(java.lang.String fieldName, java.lang.Class<?> objectClass)
public static <T> T unwrap(java.lang.Object obj, java.lang.Class<T> clazz)