public abstract class ReflectionUtils extends Object
| Constructor and Description |
|---|
ReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static Field |
findField(Class<?> clazz,
String name)
|
static Field |
findField(Class<?> clazz,
String name,
Class<?> type)
|
static Object |
getField(Field field,
Object target)
Get the field represented by the supplied
field object on the
specified target object. |
static void |
handleInvocationTargetException(InvocationTargetException ex)
Handle the given invocation target exception.
|
static void |
handleReflectionException(Exception ex)
Handle the given reflection exception.
|
static void |
makeAccessible(Field field)
Make the given field accessible, explicitly setting it accessible if
necessary.
|
static void |
rethrowRuntimeException(Throwable ex)
Rethrow the given
exception, which is presumably the
target exception of an InvocationTargetException. |
public static Field findField(Class<?> clazz, String name)
field on the supplied Class with the
supplied name. Searches all superclasses up to Object.clazz - the class to introspectname - the name of the fieldnull if not foundpublic static Field findField(Class<?> clazz, String name, Class<?> type)
field on the supplied Class with the
supplied name and/or type. Searches all superclasses
up to Object.clazz - the class to introspectname - the name of the field (may be null if type is specified)type - the type of the field (may be null if name is specified)null if not foundpublic static Object getField(Field field, Object target)
field object on the
specified target object. In accordance with 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 handleReflectionException(Exception).
field - the field to gettarget - the target object from which to get the fieldpublic 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 or UndeclaredThrowableException otherwise.
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 UndeclaredThrowableException otherwise.
ex - the invocation target exception to handlepublic static void rethrowRuntimeException(Throwable ex)
exception, which is presumably the
target exception of an 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 RuntimeException or
Error if appropriate; otherwise, throws an
UndeclaredThrowableException.
ex - the exception to rethrowRuntimeException - the rethrown exceptionpublic 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)Copyright © 2019 sejda. All rights reserved.