Class ReflectionUtils
- java.lang.Object
-
- com.broadleafcommerce.common.extension.reflection.ReflectionUtils
-
public class ReflectionUtils extends Object
Inspired by Spring'sReflectionUtils
. Simple utility class for working with the reflection API and handling reflection exceptions.
-
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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, 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
-
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
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 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
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
-
-