java.lang.Object
nl.basjes.parse.useragent.utils.springframework.util.ReflectionUtils

public abstract class ReflectionUtils extends Object
Simple utility class for working with the reflection API and handling reflection exceptions.

Only intended for internal use.

Since:
1.2.2
Author:
Juergen Hoeller, Rob Harrop, Rod Johnson, Costin Leau, Sam Brannen, Chris Beams
  • Method Details

    • 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 {link Throwable exception}, which is presumably the target exception of an {link 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 {link RuntimeException} or {link Error} if appropriate; otherwise, throws an {link UndeclaredThrowableException}.

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

      @Nullable public static Method findMethod(Class<?> clazz, String name)
      Attempt to find a {link Method} on the supplied class with the supplied name and no parameters. Searches all superclasses up to {code Object}.

      Returns {code null} if no {link Method} can be found.

      Parameters:
      clazz - the class to introspect
      name - the name of the method
      Returns:
      the Method object, or {code null} if none found
    • findMethod

      @Nullable public static Method findMethod(Class<?> clazz, String name, @Nullable Class<?>... paramTypes)
      Attempt to find a Method on the supplied class with the supplied name and parameter types. Searches all superclasses up to Object.

      Returns null if no Method can be found.

      Parameters:
      clazz - the class to introspect
      name - the name of the method
      paramTypes - the parameter types of the method (may be null to indicate any signature)
      Returns:
      the Method object, or null if none found
    • invokeMethod

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

      Thrown exceptions are handled via a call to {link #handleReflectionException}.

      Parameters:
      method - the method to invoke
      target - the target object to invoke the method on
      Returns:
      the invocation result, if any see #invokeMethod(java.lang.reflect.Method, Object, Object[])
    • invokeMethod

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

      Thrown exceptions are handled via a call to {link #handleReflectionException}.

      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
    • getField

      @Nullable public static Object getField(Field field, @Nullable Object target)
      Get the field represented by the supplied {link Field field object} on the specified {link Object target object}. In accordance with {link 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 {link #handleReflectionException(Exception)}.

      Parameters:
      field - the field to get
      target - the target object from which to get the field (or null for a static field)
      Returns:
      the field's current value