java.lang.Object
org.sejda.commons.util.ReflectionUtils
Simple utility class for working with the reflection API and handling reflection exceptions.
From Springframework
-
Method Summary
Modifier and TypeMethodDescriptionstatic Fieldstatic Fieldstatic ObjectGet the field represented by the suppliedfield objecton the specifiedtarget object.static voidHandle the given invocation target exception.static voidHandle the given reflection exception.static ClassinferParameterClass(Class clazz, String methodName) Given a concrete class and a method name, it tries to infer the Class of the first parameter of the methodstatic voidmakeAccessible(Field field) Make the given field accessible, explicitly setting it accessible if necessary.static voidRethrow the givenexception, which is presumably the target exception of anInvocationTargetException.
-
Method Details
-
findField
Attempt to find afieldon the suppliedClasswith the suppliedname. Searches all superclasses up toObject.- Parameters:
clazz- the class to introspectname- the name of the field- Returns:
- the corresponding Field object, or
nullif not found
-
findField
Attempt to find afieldon the suppliedClasswith the suppliednameand/ortype. Searches all superclasses up toObject.- Parameters:
clazz- the class to introspectname- the name of the field (may benullif type is specified)type- the type of the field (may benullif name is specified)- Returns:
- the corresponding Field object, or
nullif not found
-
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
-
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
-
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:
-
inferParameterClass
Given a concrete class and a method name, it tries to infer the Class of the first parameter of the method- Parameters:
clazz-methodName-- Returns:
- the class or null if nothing found
-