Class ReflectionUtils

java.lang.Object
org.sejda.commons.util.ReflectionUtils

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

From Springframework

  • Method Details

    • findField

      public static Field findField(Class<?> clazz, String name)
      Attempt to find a field on the supplied Class with the supplied name. Searches all superclasses up to Object.
      Parameters:
      clazz - the class to introspect
      name - the name of the field
      Returns:
      the corresponding Field object, or null if not found
    • findField

      public static Field findField(Class<?> clazz, String name, Class<?> type)
      Attempt to find a field on the supplied Class with the supplied name and/or type. Searches all superclasses up to Object.
      Parameters:
      clazz - the class to introspect
      name - the name of the field (may be null if type is specified)
      type - the type of the field (may be null if name is specified)
      Returns:
      the corresponding Field object, or null if not found
    • getField

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

      Parameters:
      field - the field to get
      target - the target object from which to get the field
      Returns:
      the field's current value
    • 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 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

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

      public static void makeAccessible(Field field)
      Make the given field accessible, explicitly setting it accessible if necessary. The setAccessible(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

      public static Class inferParameterClass(Class clazz, String methodName)
      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