Module lettuce.core

Class ReflectionUtils

java.lang.Object
io.lettuce.core.dynamic.support.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.

  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

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

      public static Method findMethod​(Class<?> clazz, String name)
      Attempt to find a Method on the supplied class with the supplied name and no parameters. 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
      Returns:
      the Method object, or null if none found
    • findMethod

      public static Method findMethod​(Class<?> clazz, String name, 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

      public static Object invokeMethod​(Method method, Object target)
      Invoke the specified Method against the supplied target object with no 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
      Returns:
      the invocation result, if any
      See Also:
      invokeMethod(java.lang.reflect.Method, Object, Object[])
    • invokeMethod

      public static Object invokeMethod​(Method method, Object target, 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 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
    • doWithMethods

      public static void doWithMethods​(Class<?> clazz, ReflectionUtils.MethodCallback mc)
      Perform the given callback operation on all matching methods of the given class and superclasses.

      The same named method occurring on subclass and superclass will appear twice, unless excluded by a ReflectionUtils.MethodFilter.

      Parameters:
      clazz - the class to introspect
      mc - the callback to invoke for each method
      See Also:
      doWithMethods(Class, MethodCallback, MethodFilter)
    • doWithMethods

      public static void doWithMethods​(Class<?> clazz, ReflectionUtils.MethodCallback mc, ReflectionUtils.MethodFilter mf)
      Perform the given callback operation on all matching methods of the given class and superclasses (or given interface and super-interfaces).

      The same named method occurring on subclass and superclass will appear twice, unless excluded by the specified ReflectionUtils.MethodFilter.

      Parameters:
      clazz - the class to introspect
      mc - the callback to invoke for each method
      mf - the filter that determines the methods to apply the callback to
    • doWithFields

      public static void doWithFields​(Class<?> clazz, ReflectionUtils.FieldCallback fc)
      Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
      Parameters:
      clazz - the target class to analyze
      fc - the callback to invoke for each field
    • doWithFields

      public static void doWithFields​(Class<?> clazz, ReflectionUtils.FieldCallback fc, ReflectionUtils.FieldFilter ff)
      Invoke the given callback on all fields in the target class, going up the class hierarchy to get all declared fields.
      Parameters:
      clazz - the target class to analyze
      fc - the callback to invoke for each field
      ff - the filter that determines the fields to apply the callback to