Class ReflectionUtils
java.lang.Object
com.broadleafcommerce.common.extension.reflection.ReflectionUtils
Inspired by Spring's
ReflectionUtils
.
Simple utility class for working with the reflection API and handling reflection exceptions.-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
Handle the given invocation target exception.static void
Handle the given reflection exception.static Object
invokeMethod
(Method method, Object target, Object... args) Invoke the specifiedMethod
against the supplied target object with the supplied arguments.static void
Rethrow the givenexception
, which is presumably the target exception of anInvocationTargetException
.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
invokeMethod
@Nullable public static Object invokeMethod(Method method, @Nullable Object target, @Nullable 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
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 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
-