Class ReflectionUtils
Only intended for internal use.
- Since:
- 1.2.2
- Author:
- Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
-
Method Summary
Modifier and TypeMethodDescriptionstatic Method
findMethod
(Class<?> clazz, String name) Attempt to find a {link Method} 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
Get the field represented by the supplied {link Field field object} on the specified {link Object target object}.static void
Handle the given invocation target exception.static void
Handle the given reflection exception.static Object
invokeMethod
(Method method, Object target) Invoke the specified {link Method} against the supplied target object with no arguments.static Object
invokeMethod
(Method method, Object target, Object... args) Invoke the specified {link Method} against the supplied target object with the supplied arguments.static void
Rethrow the given {link Throwable exception}, which is presumably the target exception of an {link InvocationTargetException}.
-
Method Details
-
handleReflectionException
Handle the given reflection exception.Should only be called if no checked exception is expected to be thrown by a target method, or if an error occurs while accessing a method or field.
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 given {link Throwable exception}, which is presumably the target exception of an {link InvocationTargetException}. Should only be called if no checked exception is expected to be thrown by the target method.Rethrows the underlying exception cast to a {link RuntimeException} or {link Error} if appropriate; otherwise, throws an {link UndeclaredThrowableException}.
- Parameters:
ex
- the exception to rethrow- Throws:
RuntimeException
- the rethrown exception
-
findMethod
Attempt to find a {link Method} on the supplied class with the supplied name and no parameters. Searches all superclasses up to {code Object}.Returns {code null} if no {link Method} can be found.
- Parameters:
clazz
- the class to introspectname
- the name of the method- Returns:
- the Method object, or {code null} if none found
-
findMethod
@Nullable public static Method findMethod(Class<?> clazz, String name, @Nullable 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
Invoke the specified {link Method} against the supplied target object with no arguments. The target object can benull
when invoking a static {link Method}.Thrown exceptions are handled via a call to {link #handleReflectionException}.
- Parameters:
method
- the method to invoketarget
- the target object to invoke the method on- Returns:
- the invocation result, if any see #invokeMethod(java.lang.reflect.Method, Object, Object[])
-
invokeMethod
@Nullable public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args) Invoke the specified {link Method} against the supplied target object with the supplied arguments. The target object can benull
when invoking a static {link Method}.Thrown exceptions are handled via a call to {link #handleReflectionException}.
- 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
-
getField
Get the field represented by the supplied {link Field field object} on the specified {link Object target object}. In accordance with {link Field#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 {link #handleReflectionException(Exception)}.
- Parameters:
field
- the field to gettarget
- the target object from which to get the field (ornull
for a static field)- Returns:
- the field's current value
-