public abstract class ReflectionUtils extends Object
Modifier and Type | Class and Description |
---|---|
static interface |
ReflectionUtils.FieldCallback
Callback interface invoked on each field in the hierarchy.
|
static interface |
ReflectionUtils.FieldFilter
Callback optionally used to filter fields to be operated on by a field callback.
|
static interface |
ReflectionUtils.MethodCallback
Action to take on each method
|
static interface |
ReflectionUtils.MethodFilter
Callback optionally used to method fields to be operated on by a method callback.
|
Modifier and Type | Field and Description |
---|---|
static ReflectionUtils.FieldFilter |
COPYABLE_FIELDS
FieldFilter that matches all non-static, non-final fields.
|
Constructor and Description |
---|
ReflectionUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
doWithFields(Class targetClass,
ReflectionUtils.FieldCallback fc)
Invoke the given callback on all private fields in the target class, going up the class
hierarchy to get all declared fields.
|
static void |
doWithFields(Class targetClass,
ReflectionUtils.FieldCallback fc,
ReflectionUtils.FieldFilter ff)
Invoke the given callback on all private fields in the target class, going up the class
hierarchy to get all declared fields.
|
static void |
doWithMethods(Class targetClass,
ReflectionUtils.MethodCallback mc)
Perform the given callback operation on all matching methods of the given class and
superclasses.
|
static void |
doWithMethods(Class targetClass,
ReflectionUtils.MethodCallback mc,
ReflectionUtils.MethodFilter mf)
Perform the given callback operation on all matching methods of the given class and
superclasses.
|
static Method[] |
getAllDeclaredMethods(Class leafClass)
Get all declared methods on the leaf class and all superclasses.
|
static Field |
getDeclaredField(Class<?> type,
String fieldName)
Gets the declared field named 'fieldName' from the 'type' class.
|
static Field |
getDeclaredFieldIncludeSuperTypes(Class<?> type,
String fieldName)
Gets the declared field named 'fieldName' from the 'type' class.
|
static void |
handleInvocationTargetException(InvocationTargetException ex)
Handle the given invocation target exception.
|
static void |
handleReflectionException(Exception ex)
Handle the given reflection exception.
|
static Object |
invokeMethod(Method method,
Object target)
|
static Object |
invokeMethod(Method method,
Object target,
Object[] args)
|
static boolean |
isCommonJavaType(Class<? extends Object> clazz) |
static boolean |
isCommonJavaType(String typeName) |
static boolean |
isNumeric(Class type)
Return
true if the specified type is numeric. |
static boolean |
isPrimitive(String typeName) |
static boolean |
isPrimitiveAssignable(String typeName,
Class<?> type) |
static boolean |
isPublicStaticFinal(Field field)
Determine whether the given field is a "public static final" constant.
|
static boolean |
isSpacePrimitive(String typeName) |
static void |
makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if necessary.
|
static void |
setField(Object target,
Field field,
Object value) |
static void |
setField(Object target,
String fieldName,
Object value) |
static void |
shallowCopyFieldState(Object src,
Object dest)
Given the source object and the destination, which must be the same class or a subclass, copy
all fields, including inherited fields.
|
public static final ReflectionUtils.FieldFilter COPYABLE_FIELDS
public static boolean isPrimitive(String typeName)
public static boolean isSpacePrimitive(String typeName)
public static boolean isCommonJavaType(String typeName)
public static void handleReflectionException(Exception ex)
Throws the underlying RuntimeException or Error in case of an InvocationTargetException with such a root cause. Throws an IllegalStateException with an appropriate message else.
ex
- the reflection exception to handlepublic static void handleInvocationTargetException(InvocationTargetException ex)
Throws the underlying RuntimeException or Error in case of such a root cause. Throws an IllegalStateException else.
ex
- the invocation target exception to handlepublic static Object invokeMethod(Method method, Object target, Object[] args)
Method
against the supplied target object with the supplied
arguments The target object can be null when invoking a static Method
. Thrown
exceptions are handled via a call to handleReflectionException(Exception)
.
public static void setField(Object target, String fieldName, Object value) throws IllegalAccessException
IllegalAccessException
public static void setField(Object target, Field field, Object value) throws IllegalAccessException
IllegalAccessException
public static Field getDeclaredFieldIncludeSuperTypes(Class<?> type, String fieldName)
public static Field getDeclaredField(Class<?> type, String fieldName) throws NoSuchFieldException
NoSuchFieldException
public static boolean isPublicStaticFinal(Field field)
field
- the field to checkpublic static void makeAccessible(Field field)
setAccessible(true)
method is only called when actually necessary, to avoid
unnecessary conflicts with a JVM SecurityManager (if active).field
- the field to make accessibleAccessibleObject.setAccessible(java.lang.reflect.AccessibleObject[], boolean)
public static void doWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc) throws IllegalArgumentException
The same named method occurring on subclass and superclass will appear twice, unless excluded by the MethodFilter
targetClass
- class to start looking atmc
- the callback to invoke for each methodIllegalArgumentException
public static void doWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf) throws IllegalArgumentException
The same named method occurring on subclass and superclass will appear twice, unless excluded by the MethodFilter
targetClass
- class to start looking atmc
- the callback to invoke for each methodmf
- the filter that determines the methods to apply the callback toIllegalArgumentException
public static Method[] getAllDeclaredMethods(Class leafClass) throws IllegalArgumentException
IllegalArgumentException
public static void doWithFields(Class targetClass, ReflectionUtils.FieldCallback fc) throws IllegalArgumentException
targetClass
- the target class to analyzefc
- the callback to invoke for each fieldIllegalArgumentException
public static void doWithFields(Class targetClass, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff) throws IllegalArgumentException
targetClass
- the target class to analyzefc
- the callback to invoke for each fieldff
- the filter that determines the fields to apply the callback toIllegalArgumentException
public static void shallowCopyFieldState(Object src, Object dest) throws IllegalArgumentException
IllegalArgumentException
- if arguments are incompatible or either is
null
public static boolean isNumeric(Class type)
Return true
if the specified type is numeric.
type
- Type to checkCopyright © GigaSpaces.