Class ReflectionUtils

java.lang.Object
dev.restate.common.reflections.ReflectionUtils

public class ReflectionUtils extends Object
  • Field Details

  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • findRestateHandlers

      public static Method @NonNull [] findRestateHandlers(Class<?> restateAnnotatedClazz)
    • findRestateAnnotatedClass

      public static Class<?> findRestateAnnotatedClass(Class<?> clazz)
      Find the class where the Restate annotations are declared.
    • extractServiceName

      public static String extractServiceName(Class<?> clazz)
    • hasServiceAnnotation

      public static boolean hasServiceAnnotation(Class<?> clazz)
    • mustHaveServiceAnnotation

      public static void mustHaveServiceAnnotation(Class<?> clazz)
    • hasVirtualObjectAnnotation

      public static boolean hasVirtualObjectAnnotation(Class<?> clazz)
    • mustHaveVirtualObjectAnnotation

      public static void mustHaveVirtualObjectAnnotation(Class<?> clazz)
    • hasWorkflowAnnotation

      public static boolean hasWorkflowAnnotation(Class<?> clazz)
    • mustHaveWorkflowAnnotation

      public static void mustHaveWorkflowAnnotation(Class<?> clazz)
    • mustHaveHandlerAnnotation

      public static ReflectionUtils.HandlerInfo mustHaveHandlerAnnotation(@NonNull Method method)
    • isKotlinClass

      public static boolean isKotlinClass(Class<?> clazz)
    • 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(@Nullable 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
    • rethrowException

      public static void rethrowException(@Nullable Throwable throwable) throws Exception
      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 an Exception or Error if appropriate; otherwise, throws an UndeclaredThrowableException.

      Parameters:
      throwable - the exception to rethrow
      Throws:
      Exception - the rethrown exception (in case of a checked exception)
    • accessibleConstructor

      public static <T> Constructor<T> accessibleConstructor(Class<T> clazz, Class<?>... parameterTypes) throws NoSuchMethodException
      Obtain an accessible constructor for the given class and parameters.
      Parameters:
      clazz - the clazz to check
      parameterTypes - the parameter types of the desired constructor
      Returns:
      the constructor reference
      Throws:
      NoSuchMethodException - if no such constructor exists
      Since:
      5.0
    • makeAccessible

      public static void makeAccessible(Constructor<?> ctor)
      Make the given constructor accessible, explicitly setting it accessible if necessary. The setAccessible(true) method is only called when actually necessary, to avoid unnecessary conflicts.
      Parameters:
      ctor - the constructor to make accessible
      See Also:
    • findMethod

      public static @Nullable 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 @Nullable Method findMethod(Class<?> clazz, String name, Class<?> @Nullable ... 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 @Nullable Object invokeMethod(Method method, @Nullable 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

      public static @Nullable 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
    • declaresException

      public static boolean declaresException(Method method, Class<?> exceptionType)
      Determine whether the given method explicitly declares the given exception or one of its superclasses, which means that an exception of that type can be propagated as-is within a reflective invocation.
      Parameters:
      method - the declaring method
      exceptionType - the exception to throw
      Returns:
      true if the exception can be thrown as-is; false if it needs to be wrapped
    • doWithLocalMethods

      public static void doWithLocalMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc)
      Perform the given callback operation on all matching methods of the given class, as locally declared or equivalent thereof (such as default methods on Java 8 based interfaces that the given class implements).
      Parameters:
      clazz - the class to introspect
      mc - the callback to invoke for each method
      Throws:
      IllegalStateException - if introspection fails
      Since:
      4.2
      See Also:
    • 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
      Throws:
      IllegalStateException - if introspection fails
      See Also:
    • doWithMethods

      public static void doWithMethods(Class<?> clazz, ReflectionUtils.MethodCallback mc, @Nullable 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
      Throws:
      IllegalStateException - if introspection fails
    • getAllDeclaredMethods

      public static Method[] getAllDeclaredMethods(Class<?> leafClass)
      Get all declared methods on the leaf class and all superclasses. Leaf class methods are included first.
      Parameters:
      leafClass - the class to introspect
      Throws:
      IllegalStateException - if introspection fails
    • getUniqueDeclaredMethods

      public static Method[] getUniqueDeclaredMethods(Class<?> leafClass)
      Get the unique set of declared methods on the leaf class and all superclasses. Leaf class methods are included first and while traversing the superclass hierarchy any methods found with signatures matching a method already included are filtered out.
      Parameters:
      leafClass - the class to introspect
      Throws:
      IllegalStateException - if introspection fails
    • getUniqueDeclaredMethods

      public static Method[] getUniqueDeclaredMethods(Class<?> leafClass, @Nullable ReflectionUtils.MethodFilter mf)
      Get the unique set of declared methods on the leaf class and all superclasses. Leaf class methods are included first and while traversing the superclass hierarchy any methods found with signatures matching a method already included are filtered out.
      Parameters:
      leafClass - the class to introspect
      mf - the filter that determines the methods to take into account
      Throws:
      IllegalStateException - if introspection fails
      Since:
      5.2
    • getDeclaredMethods

      public static Method[] getDeclaredMethods(Class<?> clazz)
      Variant of Class.getDeclaredMethods() that uses a local cache in order to avoid new Method instances. In addition, it also includes Java 8 default methods from locally implemented interfaces, since those are effectively to be treated just like declared methods.
      Parameters:
      clazz - the class to introspect
      Returns:
      the cached array of methods
      Throws:
      IllegalStateException - if introspection fails
      Since:
      5.2
      See Also:
    • isEqualsMethod

      public static boolean isEqualsMethod(@Nullable Method method)
      Determine whether the given method is an "equals" method.
      See Also:
    • isHashCodeMethod

      public static boolean isHashCodeMethod(@Nullable Method method)
      Determine whether the given method is a "hashCode" method.
      See Also:
    • isToStringMethod

      public static boolean isToStringMethod(@Nullable Method method)
      Determine whether the given method is a "toString" method.
      See Also:
    • isObjectMethod

      public static boolean isObjectMethod(@Nullable Method method)
      Determine whether the given method is originally declared by Object.
    • isCglibRenamedMethod

      public static boolean isCglibRenamedMethod(Method renamedMethod)
      Determine whether the given method is a CGLIB 'renamed' method, following the pattern "CGLIB$methodName$0".
      Parameters:
      renamedMethod - the method to check
    • clearCache

      public static void clearCache()
      Clear the internal method/field cache.
      Since:
      4.2.4