Class ReflectionUtils

java.lang.Object
com.broadleafcommerce.common.extension.reflection.ReflectionUtils

public class ReflectionUtils extends Object
Inspired by Spring's ReflectionUtils. Simple utility class for working with the reflection API and handling reflection exceptions.
  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • invokeMethod

      @Nullable public static Object invokeMethod(Method method, @Nullable Object target, @Nullable Object... args)
      Invoke the specified Method against the supplied target object with the supplied arguments. The target object can be null when invoking a static Method.

      Thrown exceptions are handled via a call to handleReflectionException(java.lang.Exception).

      Parameters:
      method - the method to invoke
      target - the target object to invoke the method on
      args - the invocation arguments (may be null)
      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 given 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.

      Parameters:
      ex - the exception to rethrow
      Throws:
      RuntimeException - the rethrown exception