public abstract class ReflectionUtils extends Object
Only intended for internal use.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceReflectionUtils.FieldCallbackCallback interface invoked on each field in the hierarchy.static interfaceReflectionUtils.FieldFilterCallback optionally used to filter fields to be operated on by a field callback.static interfaceReflectionUtils.MethodCallbackAction to take on each method.static interfaceReflectionUtils.MethodFilterCallback optionally used to filter methods to be operated on by a method callback. -
Constructor Summary
Constructors Constructor Description ReflectionUtils() -
Method Summary
Modifier and Type Method Description static voiddoWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc)Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.static voiddoWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.static voiddoWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc)Perform the given callback operation on all matching methods of the given class and superclasses.static voiddoWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf)Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).static MethodfindMethod(Class<?> clazz, String name)Attempt to find aMethodon the supplied class with the supplied name and no parameters.static MethodfindMethod(Class<?> clazz, String name, Class<?>... paramTypes)Attempt to find aMethodon the supplied class with the supplied name and parameter types.static ObjectgetField(Field field, Object target)Get the field represented by the suppliedfield objecton the specifiedtarget object.static voidhandleInvocationTargetException(InvocationTargetException ex)Handle the given invocation target exception.static voidhandleReflectionException(Exception ex)Handle the given reflection exception.static ObjectinvokeMethod(Method method, Object target)Invoke the specifiedMethodagainst the supplied target object with no arguments.static ObjectinvokeMethod(Method method, Object target, Object... args)Invoke the specifiedMethodagainst the supplied target object with the supplied arguments.static voidrethrowRuntimeException(Throwable ex)Rethrow the givenexception, which is presumably the target exception of anInvocationTargetException.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getField
Get the field represented by the suppliedfield objecton the specifiedtarget object. In accordance withField.get(Object)semantics, the returned value is automatically wrapped if the underlying field has a primitive type.Thrown exceptions are handled via a call to
handleReflectionException(Exception).- Parameters:
field- the field to gettarget- the target object from which to get the field- Returns:
- the field's current value
-
findMethod
Attempt to find aMethodon the supplied class with the supplied name and no parameters. Searches all superclasses up toObject.Returns
nullif noMethodcan be found.- Parameters:
clazz- the class to introspectname- the name of the method- Returns:
- the Method object, or
nullif none found
-
findMethod
Attempt to find aMethodon the supplied class with the supplied name and parameter types. Searches all superclasses up toObject.Returns
nullif noMethodcan be found.- Parameters:
clazz- the class to introspectname- the name of the methodparamTypes- the parameter types of the method (may benullto indicate any signature)- Returns:
- the Method object, or
nullif none found
-
invokeMethod
Invoke the specifiedMethodagainst the supplied target object with no arguments. The target object can benullwhen invoking a staticMethod.Thrown exceptions are handled via a call to
handleReflectionException(java.lang.Exception).- Parameters:
method- the method to invoketarget- the target object to invoke the method on- Returns:
- the invocation result, if any
- See Also:
invokeMethod(java.lang.reflect.Method, Object, Object[])
-
invokeMethod
Invoke the specifiedMethodagainst the supplied target object with the supplied arguments. The target object can benullwhen invoking a staticMethod.Thrown exceptions are handled via a call to
handleReflectionException(java.lang.Exception).- Parameters:
method- the method to invoketarget- the target object to invoke the method onargs- the invocation arguments (may benull)- Returns:
- the invocation result, if any
-
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 or UndeclaredThrowableException otherwise.
- 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 UndeclaredThrowableException otherwise.
- Parameters:
ex- the invocation target exception to handle
-
rethrowRuntimeException
Rethrow the givenexception, which is presumably the target exception of anInvocationTargetException. Should only be called if no checked exception is expected to be thrown by the target method.Rethrows the underlying exception cast to a
RuntimeExceptionorErrorif appropriate; otherwise, throws anUndeclaredThrowableException.- Parameters:
ex- the exception to rethrow- Throws:
RuntimeException- the rethrown exception
-
doWithMethods
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 a
ReflectionUtils.MethodFilter.- Parameters:
clazz- the class to introspectmc- the callback to invoke for each method- See Also:
doWithMethods(Class, MethodCallback, MethodFilter)
-
doWithMethods
public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf)Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified
ReflectionUtils.MethodFilter.- Parameters:
clazz- the class to introspectmc- the callback to invoke for each methodmf- the filter that determines the methods to apply the callback to
-
doWithFields
Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.- Parameters:
clazz- the target class to analyzefc- the callback to invoke for each field
-
doWithFields
public static void doWithFields(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.- Parameters:
clazz- the target class to analyzefc- the callback to invoke for each fieldff- the filter that determines the fields to apply the callback to
-