Class MethodExecutor


  • @ThreadSafe
    public final class MethodExecutor
    extends Object
    Lightweight utility class to execute a method using reflection.
    • Constructor Detail

      • MethodExecutor

        public MethodExecutor()
    • Method Detail

      • invokeDeclaredAnnotatedMethod

        public final <T> T invokeDeclaredAnnotatedMethod​(@NotNull
                                                         @NotNull Object obj,
                                                         @NotNull
                                                         @NotNull Class<? extends Annotation> annotationType,
                                                         Class<?>[] argumentTypes,
                                                         Object[] arguments)
        Finds a declared method that has an annotation and optional parameters.
        Type Parameters:
        T - Type of the return value.
        Parameters:
        obj - Object to inspect.
        annotationType - Expected annotation type.
        argumentTypes - Expected argument types, empty array or null.
        arguments - Arguments, empty array or null. Must be the same size as 'argumentTypes'.
        Returns:
        Value returned by the method or null.
      • findDeclaredAnnotatedMethod

        public final Method findDeclaredAnnotatedMethod​(@NotNull
                                                        @NotNull Object obj,
                                                        @NotNull
                                                        @NotNull Class<? extends Annotation> annotationType,
                                                        Class<?>... expectedArgumentTypes)
        Finds a declared method on the instance or it's parents that has an annotation and optional parameters. The class
        Parameters:
        obj - Object to inspect.
        annotationType - Expected annotation type.
        expectedArgumentTypes - Expected argument types, empty array or null.
        Returns:
        Method or null if any of the expected parameters does not match.
      • getDeclaredMethodsIncludingSuperClasses

        public final List<Method> getDeclaredMethodsIncludingSuperClasses​(@NotNull
                                                                          @NotNull Class<?> clasz,
                                                                          @NotNull
                                                                          @NotNull Class<?>... stopParents)
        Returns a list of declared methods from classes and super classes. The given stop classes will not be inspected.
        Parameters:
        clasz - Class to inspect.
        stopParents - Parent classes to stop inspection or null to stop at Object.
        Returns:
        List of methods.
      • invoke

        public final <T> T invoke​(@NotNull
                                  @NotNull Method method,
                                  @NotNull
                                  @NotNull Object target,
                                  Object... args)
        Invokes a method with any number of arguments.
        Type Parameters:
        T - Type of the return value.
        Parameters:
        method - Method to call.
        target - Object that contains the method.
        args - Arguments, empty array or null.
        Returns:
        Value returned by the method or null.
      • same

        public final boolean same​(Class<?>[] expected,
                                  Class<?>[] actual)
        Verifies if both arrays have the same length and types.
        Parameters:
        expected - Expected types, empty array or null.
        actual - Actual types, empty array or null.
        Returns:
        TRUE if both arguments match.