Package com.gigaspaces.internal.utils
Class ReflectionUtils
java.lang.Object
com.gigaspaces.internal.utils.ReflectionUtils
Simple utility class for handling reflection exceptions. Only intended for internal use.
- Author:
- kimchy
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface invoked on each field in the hierarchy.static interfaceCallback optionally used to filter fields to be operated on by a field callback.static interfaceAction to take on each methodstatic interfaceCallback optionally used to method fields to be operated on by a method callback. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ReflectionUtils.FieldFilterFieldFilter that matches all non-static, non-final fields. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddoWithFields(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 voiddoWithFields(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 voiddoWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc) Perform the given callback operation on all matching methods of the given class and superclasses.static voiddoWithMethods(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 FieldgetDeclaredField(Class<?> type, String fieldName) Gets the declared field named 'fieldName' from the 'type' class.static FieldgetDeclaredFieldIncludeSuperTypes(Class<?> type, String fieldName) Gets the declared field named 'fieldName' from the 'type' class.static voidHandle the given invocation target exception.static voidHandle the given reflection exception.static ObjectinvokeMethod(Method method, Object target) static ObjectinvokeMethod(Method method, Object target, Object[] args) static booleanisCommonJavaType(Class<? extends Object> clazz) static booleanisCommonJavaType(String typeName) static booleanReturntrueif the specified type is numeric.static booleanisPrimitive(String typeName) static booleanisPrimitiveAssignable(String typeName, Class<?> type) static booleanisPublicStaticFinal(Field field) Determine whether the given field is a "public static final" constant.static booleanisSpacePrimitive(String typeName) static voidmakeAccessible(Field field) Make the given field accessible, explicitly setting it accessible if necessary.static voidstatic voidstatic voidshallowCopyFieldState(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.
-
Field Details
-
COPYABLE_FIELDS
FieldFilter that matches all non-static, non-final fields.
-
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
isPrimitive
-
isPrimitiveAssignable
-
isSpacePrimitive
-
isCommonJavaType
-
isCommonJavaType
-
handleReflectionException
Handle the given reflection exception. Should only be called if no checked exception is expected to be thrown by the target method.Throws the underlying RuntimeException or Error in case of an InvocationTargetException with such a root cause. Throws an IllegalStateException with an appropriate message else.
- Parameters:
ex- the reflection exception to handle
-
handleInvocationTargetException
Handle the given invocation target exception. Should only be called if no checked exception is expected to be thrown by the target method.Throws the underlying RuntimeException or Error in case of such a root cause. Throws an IllegalStateException else.
- Parameters:
ex- the invocation target exception to handle
-
invokeMethod
-
invokeMethod
Invoke the specifiedMethodagainst the supplied target object with the supplied arguments The target object can be null when invoking a staticMethod.Thrown exceptions are handled via a call to
handleReflectionException(Exception). -
setField
public static void setField(Object target, String fieldName, Object value) throws IllegalAccessException - Throws:
IllegalAccessException
-
setField
- Throws:
IllegalAccessException
-
getDeclaredFieldIncludeSuperTypes
Gets the declared field named 'fieldName' from the 'type' class. If 'type' does not declare such field, tries to get to get this field from 'type' parent class. This procedure continues until a matching field is found or until Object is reached in which case, null is returned. -
getDeclaredField
Gets the declared field named 'fieldName' from the 'type' class.- Throws:
NoSuchFieldException
-
isPublicStaticFinal
Determine whether the given field is a "public static final" constant.- Parameters:
field- the field to check
-
makeAccessible
Make the given field accessible, explicitly setting it accessible if necessary. ThesetAccessible(true)method is only called when actually necessary, to avoid unnecessary conflicts with a JVM SecurityManager (if active).- Parameters:
field- the field to make accessible- See Also:
-
doWithMethods
public static void doWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc) throws IllegalArgumentException Perform the given callback operation on all matching methods of the given class and superclasses.The same named method occurring on subclass and superclass will appear twice, unless excluded by the MethodFilter
- Parameters:
targetClass- class to start looking atmc- the callback to invoke for each method- Throws:
IllegalArgumentException
-
doWithMethods
public static void doWithMethods(Class targetClass, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf) throws IllegalArgumentException Perform the given callback operation on all matching methods of the given class and superclasses.The same named method occurring on subclass and superclass will appear twice, unless excluded by the MethodFilter
- Parameters:
targetClass- class to start looking atmc- the callback to invoke for each methodmf- the filter that determines the methods to apply the callback to- Throws:
IllegalArgumentException
-
getAllDeclaredMethods
Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.- Throws:
IllegalArgumentException
-
doWithFields
public static void doWithFields(Class targetClass, ReflectionUtils.FieldCallback fc) throws IllegalArgumentException Invoke the given callback on all private fields in the target class, going up the class hierarchy to get all declared fields.- Parameters:
targetClass- the target class to analyzefc- the callback to invoke for each field- Throws:
IllegalArgumentException
-
doWithFields
public static void doWithFields(Class targetClass, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff) throws IllegalArgumentException Invoke the given callback on all private fields in the target class, going up the class hierarchy to get all declared fields.- Parameters:
targetClass- the target class to analyzefc- the callback to invoke for each fieldff- the filter that determines the fields to apply the callback to- Throws:
IllegalArgumentException
-
shallowCopyFieldState
Given the source object and the destination, which must be the same class or a subclass, copy all fields, including inherited fields. Designed to work on objects with public no-arg constructors.- Throws:
IllegalArgumentException- if arguments are incompatible or either isnull
-
isNumeric
Return
trueif the specified type is numeric.- Parameters:
type- Type to check
-