Class ReflectionUtils
- java.lang.Object
-
- io.lettuce.core.dynamic.support.ReflectionUtils
-
public abstract class ReflectionUtils extends Object
Simple utility class for working with the reflection API and handling reflection exceptions.Only intended for internal use.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 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 filter methods to be operated on by a method callback.
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
doWithFields(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 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.static void
doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc)
Perform the given callback operation on all matching methods of the given class and superclasses.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).static Method
findMethod(Class<?> clazz, String name)
Attempt to find aMethod
on the supplied class with the supplied name and no parameters.static Method
findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
Attempt to find aMethod
on the supplied class with the supplied name and parameter types.static Object
getField(Field field, Object target)
Get the field represented by the suppliedfield object
on the specifiedtarget object
.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)
Invoke the specifiedMethod
against the supplied target object with no arguments.static Object
invokeMethod(Method method, Object target, Object... args)
Invoke the specifiedMethod
against the supplied target object with the supplied arguments.static void
rethrowRuntimeException(Throwable ex)
Rethrow the givenexception
, which is presumably the target exception of anInvocationTargetException
.
-
-
-
Method Detail
-
getField
public static Object getField(Field field, Object target)
Get the field represented by the suppliedfield object
on 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
public static Method findMethod(Class<?> clazz, String name)
Attempt to find aMethod
on the supplied class with the supplied name and no parameters. Searches all superclasses up toObject
.Returns
null
if noMethod
can be found.- Parameters:
clazz
- the class to introspectname
- the name of the method- Returns:
- the Method object, or
null
if none found
-
findMethod
public static Method findMethod(Class<?> clazz, String name, Class<?>... paramTypes)
Attempt to find aMethod
on the supplied class with the supplied name and parameter types. Searches all superclasses up toObject
.Returns
null
if noMethod
can be found.- Parameters:
clazz
- the class to introspectname
- the name of the methodparamTypes
- the parameter types of the method (may benull
to indicate any signature)- Returns:
- the Method object, or
null
if none found
-
invokeMethod
public static Object invokeMethod(Method method, Object target)
Invoke the specifiedMethod
against the supplied target object with no arguments. The target object can benull
when 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
public static Object invokeMethod(Method method, Object target, Object... args)
Invoke the specifiedMethod
against the supplied target object with the supplied arguments. The target object can benull
when 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
public static void handleReflectionException(Exception ex)
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
public static void handleInvocationTargetException(InvocationTargetException ex)
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
public static void rethrowRuntimeException(Throwable ex)
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
RuntimeException
orError
if appropriate; otherwise, throws anUndeclaredThrowableException
.- Parameters:
ex
- the exception to rethrow- Throws:
RuntimeException
- the rethrown exception
-
doWithMethods
public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc)
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
public static void doWithFields(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.- 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
-
-