Class MethodUtils

  • All Implemented Interfaces:
    Utils

    public abstract class MethodUtils
    extends java.lang.Object
    implements Utils
    The Java Reflection Method Utility class
    Since:
    1.0.0
    Author:
    Mercy
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Method banMethod​(java.lang.Class<?> declaredClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Bans method to the cache based on the provided class, method name, and parameter types.
      static java.lang.String buildSignature​(java.lang.Class<?> declaringClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Builds a method signature string based on the provided declaring class, method name, and parameter types.
      static void clearBannedMethods()
      Clears the cache of banned methods.
      static java.util.function.Predicate<? super java.lang.reflect.Method> excludedDeclaredClass​(java.lang.Class<?> declaredClass)
      Creates a Predicate that excludes methods declared by the specified class.
      static java.util.List<java.lang.reflect.Method> findAllDeclaredMethods​(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Retrieves all declared methods directly defined in the specified class, excluding inherited methods.
      static java.util.List<java.lang.reflect.Method> findAllMethods​(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Get all public methods of the target class, including the inherited methods.
      static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Finds a declared method in the specified class, including its superclasses and interfaces.
      static java.util.List<java.lang.reflect.Method> findDeclaredMethods​(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Find all declared methods of the target class, excluding the inherited methods.
      static java.lang.reflect.Method findMethod​(java.lang.Class targetClass, java.lang.String methodName)
      Find the Method by the specified type (including inherited types) and method name without the parameter type.
      static java.lang.reflect.Method findMethod​(java.lang.Class targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Find the Method by the specified type (including inherited types), method name, and parameter types.
      static java.util.List<java.lang.reflect.Method> findMethods​(java.lang.Class<?> targetClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Find all methods of the target class by the specified criteria.
      static java.util.List<java.lang.reflect.Method> findMethods​(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Find all public methods directly declared in the specified class, without including inherited methods.
      static java.lang.reflect.Method findNearestOverriddenMethod​(java.lang.reflect.Method overrider)
      Finds the nearest overridden method in the class hierarchy for the given overriding method.
      static java.lang.reflect.Method findOverriddenMethod​(java.lang.reflect.Method overrider, java.lang.Class<?> targetClass)
      Finds the method in the specified target class that is overridden by the given overriding method.
      static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> targetClass)
      Get all declared methods of the target class, including the inherited methods.
      static java.util.List<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> targetClass)
      Get all public methods of the target class, including the inherited methods.
      static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> targetClass)
      Get all declared methods of the target class, excluding the inherited methods.
      static java.util.List<java.lang.reflect.Method> getMethods​(java.lang.Class<?> targetClass)
      Get all public methods of the target class, excluding the inherited methods.
      static java.lang.String getSignature​(java.lang.reflect.Method method)
      Generates a string representation of the method signature.
      static void initBannedMethods()
      Initializes the banned methods cache based on the system property "microsphere.reflect.banned-methods".
      static <R> R invokeMethod​(java.lang.Object instance, java.lang.Class<?> type, java.lang.String methodName, java.lang.Object... arguments)
      Invokes a method with the specified name on the given class type, using the provided arguments.
      static <R> R invokeMethod​(java.lang.Object instance, java.lang.reflect.Method method, java.lang.Object... arguments)
      Invokes the underlying method represented by this Method object, on the specified object with the specified parameters.
      static <R> R invokeMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Object... arguments)
      Invokes a method with the specified name on the given object, using the provided arguments.
      static <R> R invokeStaticMethod​(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... arguments)
      Invokes a static method of the specified target class with the given method name and arguments.
      static <R> R invokeStaticMethod​(java.lang.reflect.Method method, java.lang.Object... arguments)
      Invokes the specified static method represented by the given Method object.
      static boolean isCallerSensitiveMethod​(java.lang.reflect.Method method)
      Checks if the specified method is annotated with jdk.internal.reflect.CallerSensitive.
      static boolean isObjectMethod​(java.lang.reflect.Method method)
      Checks whether the given method is declared by the Object class.
      static boolean overrides​(java.lang.reflect.Method overrider, java.lang.reflect.Method overridden)
      Tests whether one method, as a member of a given type, overrides another method.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BANNED_METHODS_PROPERTY_NAME

        public static final java.lang.String BANNED_METHODS_PROPERTY_NAME
        The property name of banned methods

        Example Usage

        
         System.setProperty(BANNED_METHODS_PROPERTY_NAME, "java.lang.String#substring() | java.lang.String#substring(int,int)")
         Method method = MethodUtils.findMethod(String.class, "substring", int.class, int.class); // returns null
         method = MethodUtils.findMethod(String.class, "substring"); // returns null
         method = MethodUtils.findMethod(String.class, "substring", int.class); // returns non-null
         
        See Also:
        Constant Field Values
      • OBJECT_PUBLIC_METHODS

        @Nonnull
        @Immutable
        public static final java.util.List<java.lang.reflect.Method> OBJECT_PUBLIC_METHODS
        The public methods of Object
      • OBJECT_DECLARED_METHODS

        @Nonnull
        @Immutable
        public static final java.util.List<java.lang.reflect.Method> OBJECT_DECLARED_METHODS
        The declared methods of Object
      • OBJECT_METHOD_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Method> OBJECT_METHOD_PREDICATE
        The Predicate reference to isObjectMethod(Method)
      • PUBLIC_METHOD_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Method> PUBLIC_METHOD_PREDICATE
        The Predicate reference to MemberUtils.isPublic(Member)
      • STATIC_METHOD_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Method> STATIC_METHOD_PREDICATE
        The Predicate reference to MemberUtils.isStatic(Member)
      • NON_STATIC_METHOD_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Method> NON_STATIC_METHOD_PREDICATE
        The Predicate reference to MemberUtils.isNonStatic(Member)
      • FINAL_METHOD_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Method> FINAL_METHOD_PREDICATE
        The Predicate reference to MemberUtils.isFinal(Member)
      • NON_PRIVATE_METHOD_PREDICATE

        public static final java.util.function.Predicate<? super java.lang.reflect.Method> NON_PRIVATE_METHOD_PREDICATE
        The Predicate reference to MemberUtils.isNonPrivate(Member)
    • Method Detail

      • initBannedMethods

        public static void initBannedMethods()
        Initializes the banned methods cache based on the system property "microsphere.reflect.banned-methods".

        This method reads the comma-separated list of method signatures from the system property "microsphere.reflect.banned-methods", parses each signature, resolves the corresponding Method, and stores it in the bannedMethodsCache. The format for each method signature is: fully.qualified.ClassName#methodName(parameter.Type1,parameter.Type2).

        Example Usage

        
         // Set the system property to ban specific methods
         System.setProperty(MethodUtils.BANNED_METHODS_PROPERTY_NAME,
             "java.lang.String#substring(int,int)|java.lang.Object#toString()");
        
         // Initialize the banned methods cache
         MethodUtils.initBannedMethods();
        
         // After this call, findMethod will return null for banned methods
         Method substringMethod = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
         // substringMethod will be null
         
      • banMethod

        public static java.lang.reflect.Method banMethod​(java.lang.Class<?> declaredClass,
                                                         java.lang.String methodName,
                                                         java.lang.Class<?>... parameterTypes)
        Bans method to the cache based on the provided class, method name, and parameter types.

        This method creates a MethodUtils.MethodKey using the specified parameters, finds the corresponding Method using doFindMethod(MethodKey), and stores it in the bannedMethodsCache. If the method is already present in the cache, it will not be overwritten.

        Example Usage

        
         // Ban the String.substring(int, int) method
         MethodUtils.banMethod(String.class, "substring", int.class, int.class);
        
         // After this call, findMethod will return null for the banned method
         Method method = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
         // method will be null
         
        Parameters:
        declaredClass - the class that declares the method to be banned
        methodName - the name of the method to be banned
        parameterTypes - the parameter types of the method to be banned (can be empty if the method has no parameters)
        Returns:
        the banned method, or null if the method could not be found
        Throws:
        java.lang.NullPointerException - If the target method can't be found
        See Also:
        initBannedMethods(), bannedMethodsCache
      • clearBannedMethods

        public static void clearBannedMethods()
        Clears the cache of banned methods.

        This method removes all entries from the bannedMethodsCache, effectively allowing all previously banned methods to be discoverable again by findMethod(Class, String, Class[]).

        Example Usage

        
         // Clear all banned methods
         MethodUtils.clearBannedMethods();
        
         // After this call, findMethod will return previously banned methods
         Method method = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
         // method may now be non-null if it was previously banned
         
        See Also:
        initBannedMethods(), bannedMethodsCache
      • excludedDeclaredClass

        @Nonnull
        public static java.util.function.Predicate<? super java.lang.reflect.Method> excludedDeclaredClass​(java.lang.Class<?> declaredClass)
        Creates a Predicate that excludes methods declared by the specified class.

        This method is useful when filtering methods to exclude those that are declared by a specific class, for example, when searching for overridden methods in subclasses.

        Example Usage

        
         // Find all methods in MyClass excluding those declared by MySuperClass
         List<Method> filteredMethods = MethodUtils.findMethods(MyClass.class,
             MethodUtils.excludedDeclaredClass(MySuperClass.class));
         
        Parameters:
        declaredClass - the class whose declared methods should be excluded
        Returns:
        a non-null Predicate that evaluates to true for methods not declared by the given class
      • getDeclaredMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> targetClass)
        Get all declared methods of the target class, excluding the inherited methods.

        This method retrieves only the methods that are directly declared in the specified class, without including any methods from its superclasses or interfaces.

        Example Usage

        
         // Get all declared methods in MyClass
         List<Method> declaredMethods = MethodUtils.getDeclaredMethods(MyClass.class);
         
        Parameters:
        targetClass - the target class
        Returns:
        non-null read-only List of declared methods
        See Also:
        findDeclaredMethods(Class, Predicate...)
      • getMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> getMethods​(java.lang.Class<?> targetClass)
        Get all public methods of the target class, excluding the inherited methods.

        This method retrieves only the public methods that are directly declared in the specified class, without including any methods from its superclasses or interfaces.

        Example Usage

        
         // Get all public methods declared in MyClass
         List<Method> publicMethods = MethodUtils.getMethods(MyClass.class);
         
        Parameters:
        targetClass - the target class
        Returns:
        non-null read-only List
        See Also:
        findMethods(Class, Predicate...)
      • getAllDeclaredMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> targetClass)
        Get all declared methods of the target class, including the inherited methods.

        This method retrieves all methods that are declared in the specified class and its superclasses, including those from interfaces implemented by the class and its ancestors.

        Example Usage

        
         // Get all declared methods in MyClass, including inherited ones
         List<Method> allDeclaredMethods = MethodUtils.getAllDeclaredMethods(MyClass.class);
         
        Parameters:
        targetClass - the target class
        Returns:
        non-null read-only List
        See Also:
        findAllDeclaredMethods(Class, Predicate...)
      • getAllMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> targetClass)
        Get all public methods of the target class, including the inherited methods.

        This method retrieves all public methods that are declared in the specified class and its superclasses, including those from interfaces implemented by the class and its ancestors.

        Example Usage

        
         // Get all public methods of MyClass, including inherited ones
         List<Method> allPublicMethods = MethodUtils.getAllMethods(MyClass.class);
         

        Note: If you need only the methods declared directly in the class (excluding inherited ones), consider using getMethods(Class) instead.

        Parameters:
        targetClass - the target class
        Returns:
        non-null read-only List
        See Also:
        findAllMethods(Class, Predicate...)
      • findDeclaredMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> findDeclaredMethods​(java.lang.Class<?> targetClass,
                                                                                   java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Find all declared methods of the target class, excluding the inherited methods.

        This method retrieves only the methods that are directly declared in the specified class, without including any methods from its superclasses or interfaces.

        Example Usage

        
         // Get all declared methods in MyClass
         List<Method> declaredMethods = MethodUtils.findDeclaredMethods(MyClass.class);
         

        Filtering Example

        
         // Get all non-private declared methods in MyClass
         List<Method> nonPrivateMethods = MethodUtils.findDeclaredMethods(MyClass.class,
             MethodUtils::isNonPrivate);
         
        Parameters:
        targetClass - the target class
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
        See Also:
        findMethods(Class, boolean, boolean, Predicate[])
      • findMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> findMethods​(java.lang.Class<?> targetClass,
                                                                           java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Find all public methods directly declared in the specified class, without including inherited methods.

        This method retrieves only the public methods that are explicitly declared in the given class, excluding any methods from its superclasses or interfaces.

        Example Usage

        
         // Get all public methods declared in MyClass
         List<Method> publicMethods = MethodUtils.findMethods(MyClass.class);
         

        Filtering Example

        
         // Get all non-static public methods declared in MyClass
         List<Method> nonStaticPublicMethods = MethodUtils.findMethods(MyClass.class,
             method -> !MemberUtils.isStatic(method));
         
        Parameters:
        targetClass - the target class to inspect
        methodsToFilter - optional predicates used to filter the methods further
        Returns:
        a non-null read-only list of public methods declared in the specified class
      • findAllDeclaredMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> findAllDeclaredMethods​(java.lang.Class<?> targetClass,
                                                                                      java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Retrieves all declared methods directly defined in the specified class, excluding inherited methods.

        This method returns only the methods that are explicitly declared in the given class, and does not include any methods from superclasses or interfaces.

        Example Usage

        
         // Get all declared methods in MyClass
         List<Method> declaredMethods = MethodUtils.findAllDeclaredMethods(MyClass.class);
         

        Filtering Example

        
         // Get all non-private declared methods in MyClass
         List<Method> nonPrivateMethods = MethodUtils.findAllDeclaredMethods(MyClass.class,
             method -> !MemberUtils.isPrivate(method));
         
        Parameters:
        targetClass - the class to retrieve declared methods from
        methodsToFilter - optional predicates to filter the methods
        Returns:
        a non-null read-only list of declared methods in the specified class
      • findAllMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> findAllMethods​(java.lang.Class<?> targetClass,
                                                                              java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Get all public methods of the target class, including the inherited methods.

        This method retrieves all public methods that are declared in the specified class and its superclasses, including those from interfaces implemented by the class and its ancestors.

        Example Usage

        
         // Get all public methods of MyClass, including inherited ones
         List<Method> allPublicMethods = MethodUtils.findAllMethods(MyClass.class);
         

        Filtering Example

        
         // Get all non-static public methods of MyClass, including inherited ones
         List<Method> nonStaticPublicMethods = MethodUtils.findAllMethods(MyClass.class,
             method -> !MemberUtils.isStatic(method));
         
        Parameters:
        targetClass - the target class
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
      • findMethods

        @Nonnull
        @Immutable
        public static java.util.List<java.lang.reflect.Method> findMethods​(java.lang.Class<?> targetClass,
                                                                           boolean includeInheritedTypes,
                                                                           boolean publicOnly,
                                                                           java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Find all methods of the target class by the specified criteria.

        This method provides a flexible way to retrieve methods from a class based on whether inherited methods should be included, whether only public methods should be considered, and optional filtering predicates.

        Example Usage

        Basic Usage

        
         // Get all public methods of MyClass including inherited ones
         List<Method> methods = MethodUtils.findMethods(MyClass.class, true, true);
         

        Filtering Example

        
         // Get all non-static public methods of MyClass including inherited ones
         List<Method> nonStaticPublicMethods = MethodUtils.findMethods(MyClass.class, true, true,
             method -> !MemberUtils.isStatic(method));
         

        Advanced Filtering Example

        
         // Get all non-private, non-static methods of MyClass including inherited ones
         List<Method> filteredMethods = MethodUtils.findMethods(MyClass.class, true, false,
             MethodUtils::isNonPrivate, MemberUtils::isNonStatic);
         
        Parameters:
        targetClass - the target class
        includeInheritedTypes - if set to true, includes methods from superclasses and interfaces
        publicOnly - if set to true, only public methods are returned
        methodsToFilter - (optional) one or more predicates to further filter the methods
        Returns:
        a non-null read-only list of methods matching the criteria
      • findMethod

        @Nullable
        public static java.lang.reflect.Method findMethod​(java.lang.Class targetClass,
                                                          java.lang.String methodName)
        Find the Method by the specified type (including inherited types) and method name without the parameter type.

        This method searches for a method with the given name in the specified class and its superclasses, returning the first match found. If no method is found, this method returns null.

        Example Usage

        
         // Find a method named "toString" in the MyClass class
         Method method = MethodUtils.findMethod(MyClass.class, "toString");
         if (method != null) {
             System.out.println("Method found: " + method);
         } else {
             System.out.println("Method not found.");
         }
         
        Parameters:
        targetClass - the target type to search for the method
        methodName - the name of the method to find
        Returns:
        the found method, or null if no matching method is found
      • findMethod

        @Nullable
        public static java.lang.reflect.Method findMethod​(java.lang.Class targetClass,
                                                          java.lang.String methodName,
                                                          java.lang.Class<?>... parameterTypes)
        Find the Method by the specified type (including inherited types), method name, and parameter types.

        This method searches for a method with the given name and parameter types in the specified class and its superclasses, returning the first match found. The search is cached to improve performance on repeated calls. If no matching method is found, this method returns null.

        Example Usage

        
         // Find a method named "toString" with no parameters in MyClass
         Method method = MethodUtils.findMethod(MyClass.class, "toString");
         if (method != null) {
             System.out.println("Method found: " + method);
         } else {
             System.out.println("Method not found.");
         }
         
        
         // Find a method named "setValue" that takes a String parameter
         Method method = MethodUtils.findMethod(MyClass.class, "setValue", String.class);
         if (method != null) {
             System.out.println("Method found: " + method);
         }
         
        Parameters:
        targetClass - the target class to search for the method
        methodName - the name of the method to find
        parameterTypes - the parameter types of the method (optional, defaults to empty array)
        Returns:
        the found method, or null if no matching method is found
      • findDeclaredMethod

        @Nullable
        public static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class<?> targetClass,
                                                                  java.lang.String methodName,
                                                                  java.lang.Class<?>... parameterTypes)
        Finds a declared method in the specified class, including its superclasses and interfaces.

        This method searches for a method with the given name and parameter types in the specified class, its superclasses (if the class is not an interface), and its implemented interfaces. It returns the first matching method found.

        Example Usage

        
         // Find a method named "exampleMethod" with no parameters
         Method method1 = MethodUtils.findDeclaredMethod(MyClass.class, "exampleMethod");
         
        
         // Find a method named "exampleMethod" that takes a String and an int
         Method method2 = MethodUtils.findDeclaredMethod(MyClass.class, "exampleMethod", String.class, int.class);
         
        Parameters:
        targetClass - the class to search for the declared method
        methodName - the name of the method to find
        parameterTypes - the parameter types of the method (optional, defaults to empty array)
        Returns:
        the found method, or null if no matching method is found
      • invokeMethod

        @Nullable
        public static <R> R invokeMethod​(java.lang.Object object,
                                         java.lang.String methodName,
                                         java.lang.Object... arguments)
        Invokes a method with the specified name on the given object, using the provided arguments.

        This method dynamically retrieves the class of the target object and searches for the appropriate method to invoke based on the method name and argument types. It supports both instance and static methods.

        Example Usage

        
         // Example class with an instance method
         public class MyClass {
             public String greet(String name) {
                 return "Hello, " + name;
             }
         }
        
         // Create an instance of MyClass
         MyClass myInstance = new MyClass();
        
         // Call the 'greet' method using invokeMethod
         String result = MethodUtils.invokeMethod(myInstance, "greet", "World");
         System.out.println(result);  // Output: Hello, World
         

        Note: This method internally uses reflection to find and invoke the matching method, which may throw exceptions if the method cannot be found or invoked properly.

        Type Parameters:
        R - The expected return type of the method.
        Parameters:
        object - The object on which the method will be invoked. Must not be null.
        methodName - The name of the method to invoke. Must not be null or empty.
        arguments - The arguments to pass to the method. Can be null or empty if the method requires no parameters.
        Returns:
        The result of invoking the method, wrapped in the appropriate type.
        Throws:
        java.lang.NullPointerException - If the provided object is null.
        java.lang.IllegalStateException - If the method cannot be found or accessed.
        java.lang.IllegalArgumentException - If the arguments do not match the method's parameter types.
        java.lang.RuntimeException - If the underlying method throws an exception during invocation.
      • invokeStaticMethod

        @Nullable
        public static <R> R invokeStaticMethod​(java.lang.Class<?> targetClass,
                                               java.lang.String methodName,
                                               java.lang.Object... arguments)
        Invokes a static method of the specified target class with the given method name and arguments.

        This utility method simplifies the process of invoking a static method using reflection by internally calling invokeMethod(Object, Class, String, Object...) with a null instance to indicate that the method is static.

        Example Usage

        
         public class ExampleClass {
             public static int add(int a, int b) {
                 return a + b;
             }
         }
        
         // Invoke the static method "add"
         Integer result = MethodUtils.invokeStaticMethod(ExampleClass.class, "add", 2, 3);
         System.out.println(result);  // Output: 5
         
        Type Parameters:
        R - the expected return type of the method
        Parameters:
        targetClass - the class containing the static method
        methodName - the name of the static method to invoke
        arguments - the arguments to pass to the method (can be null or empty)
        Returns:
        the result of the method invocation, wrapped in the appropriate type
        Throws:
        java.lang.NullPointerException - if the provided target class or method name is null
        java.lang.IllegalStateException - if the method cannot be found or accessed
        java.lang.IllegalArgumentException - if the arguments do not match the method's parameter types
        java.lang.RuntimeException - if the underlying method throws an exception during invocation
      • invokeStaticMethod

        @Nullable
        public static <R> R invokeStaticMethod​(java.lang.reflect.Method method,
                                               java.lang.Object... arguments)
        Invokes the specified static method represented by the given Method object.

        This method is specifically designed to invoke static methods. If the provided method is not static, it may result in an exception during invocation.

        Example Usage

        
         public class ExampleClass {
             public static int multiply(int a, int b) {
                 return a * b;
             }
         }
        
         // Retrieve the method using reflection
         Method method = ExampleClass.class.getMethod("multiply", int.class, int.class);
        
         // Invoke the static method
         Integer result = MethodUtils.invokeStaticMethod(method, 5, 3);
         System.out.println(result);  // Output: 15
         
        Type Parameters:
        R - the expected return type of the method
        Parameters:
        method - the Method object representing the static method to be invoked
        arguments - the arguments to pass to the method (can be null or empty)
        Returns:
        the result of the method invocation, wrapped in the appropriate type
        Throws:
        java.lang.NullPointerException - if the provided method is null
        java.lang.IllegalStateException - if the method cannot be accessed or throws an exception during invocation
        java.lang.IllegalArgumentException - if the arguments do not match the method's parameter types
      • invokeMethod

        @Nullable
        public static <R> R invokeMethod​(java.lang.Object instance,
                                         java.lang.Class<?> type,
                                         java.lang.String methodName,
                                         java.lang.Object... arguments)
        Invokes a method with the specified name on the given class type, using the provided arguments.

        This method dynamically searches for a method in the specified class that matches the method name and argument types, and then invokes it. It supports both instance and static methods.

        Example Usage

        
         public class ExampleClass {
             public String greet(String name) {
                 return "Hello, " + name;
             }
         }
        
         // Create an instance of ExampleClass
         ExampleClass exampleInstance = new ExampleClass();
        
         // Call the 'greet' method using invokeMethod
         String result = MethodUtils.invokeMethod(exampleInstance, ExampleClass.class, "greet", "World");
         System.out.println(result);  // Output: Hello, World
         

        Note: This method internally uses reflection to find and invoke the matching method, which may throw exceptions if the method cannot be found or invoked properly.

        Type Parameters:
        R - The expected return type of the method.
        Parameters:
        instance - The object on which the method will be invoked. Can be null for static methods.
        type - The class type to search for the method. Must not be null.
        methodName - The name of the method to invoke. Must not be null or empty.
        arguments - The arguments to pass to the method. Can be null or empty if the method requires no parameters.
        Returns:
        The result of invoking the method, wrapped in the appropriate type.
        Throws:
        java.lang.NullPointerException - If the provided type or method name is null.
        java.lang.IllegalStateException - If the method cannot be found or accessed.
        java.lang.IllegalArgumentException - If the arguments do not match the method's parameter types.
        java.lang.RuntimeException - If the underlying method throws an exception during invocation.
      • invokeMethod

        @Nullable
        public static <R> R invokeMethod​(@Nullable
                                         java.lang.Object instance,
                                         java.lang.reflect.Method method,
                                         java.lang.Object... arguments)
        Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary.

        If the underlying method is static, then the specified instance argument is ignored. It may be null.

        If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null.

        If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, section ; in particular, overriding based on the runtime type of the target object may occur.

        If the underlying method is static, the class that declared the method is initialized if it has not already been initialized.

        If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. However, if the value has the type of an array of a primitive type, the elements of the array are not wrapped in objects; in other words, an array of primitive type is returned. If the underlying method return type is void, the invocation returns null.

        Example Usage

        
         public class ExampleClass {
             public String greet(String name) {
                 return "Hello, " + name;
             }
        
             public static int add(int a, int b) {
                 return a + b;
             }
         }
        
         // Instance method example
         ExampleClass instance = new ExampleClass();
         String result = MethodUtils.invokeMethod(instance, ExampleClass.class.getMethod("greet", String.class), "World");
         System.out.println(result); // Output: Hello, World
        
         // Static method example
         Integer sum = MethodUtils.invokeMethod(null, ExampleClass.class.getMethod("add", int.class, int.class), 2, 3);
         System.out.println(sum); // Output: 5
         
        Type Parameters:
        R -
        Parameters:
        instance - the object the underlying method is invoked from
        method - the underlying method
        arguments - the arguments used for the method call
        Returns:
        the result of dispatching the method represented by this object on instance with parameters arguments
        Throws:
        java.lang.NullPointerException - if this Method object is null
        java.lang.IllegalStateException - if this Method object is enforcing Java language access control and the underlying method is inaccessible.
        java.lang.IllegalArgumentException - if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion.
        java.lang.RuntimeException - if the underlying method throws an exception.
      • overrides

        public static boolean overrides​(java.lang.reflect.Method overrider,
                                        java.lang.reflect.Method overridden)
        Tests whether one method, as a member of a given type, overrides another method.

        This method checks if the first method (overrider) overrides the second method (overridden). For a method to override another method, it must meet several conditions based on the Java Language Specification, such as being declared in a subclass of the declaring class of the overridden method, having the same name and signature, not being private or static, and having a return type that is a subtype of the overridden method's return type.

        Example Usage

        {@code
         class Parent {
             public void sayHello() {
                 System.out.println("Hello from Parent");
             }
         }
        
         class Child extends Parent {
        Parameters:
        overrider - the method that may override the other method
        overridden - the method that may be overridden
        Returns:
        true if and only if the first method overrides the second
      • findNearestOverriddenMethod

        @Nullable
        public static java.lang.reflect.Method findNearestOverriddenMethod​(java.lang.reflect.Method overrider)
        Finds the nearest overridden method in the class hierarchy for the given overriding method.

        This method searches through the inheritance chain of the class that declares the provided overriding method to locate the first method it overrides. The search includes both superclasses and interfaces.

        Example Usage

        {@code
         public class Parent {
             public void exampleMethod() {}
         }
        
         public class Child extends Parent {
        Parameters:
        overrider - the method that potentially overrides another method
        Returns:
        the overridden method if found; otherwise, null
      • findOverriddenMethod

        @Nullable
        public static java.lang.reflect.Method findOverriddenMethod​(java.lang.reflect.Method overrider,
                                                                    java.lang.Class<?> targetClass)
        Finds the method in the specified target class that is overridden by the given overriding method.

        This method searches for a method in the provided targetClass with the same name and signature as the overrider method, and checks whether it is actually overridden by the provided method. If a matching overridden method is found, it is returned; otherwise, this method returns null.

        Example Usage

        {@code
         public class Parent {
             public void exampleMethod(String arg) {
                 System.out.println("Parent method");
             }
         }
        
         public class Child extends Parent {
        Parameters:
        overrider - the method that potentially overrides another method
        targetClass - the class where the overridden method might be declared
        Returns:
        the overridden method declared in the target class, or null if none is found
      • getSignature

        @Nonnull
        public static java.lang.String getSignature​(java.lang.reflect.Method method)
        Generates a string representation of the method signature.

        The signature includes the fully qualified name of the declaring class, the method name, and the parameter types in parentheses.

        Example Usage

        
         Method method = String.class.getMethod("substring", int.class, int.class);
         String signature = MethodUtils.getSignature(method);
         System.out.println(signature);  // Output: java.lang.String#substring(int,int)
         
        Parameters:
        method - The method for which to generate the signature.
        Returns:
        A non-null string representing the method signature.
      • buildSignature

        public static java.lang.String buildSignature​(java.lang.Class<?> declaringClass,
                                                      java.lang.String methodName,
                                                      java.lang.Class<?>... parameterTypes)
        Builds a method signature string based on the provided declaring class, method name, and parameter types.

        The generated signature follows the format: declaringClassName#methodName(paramType1,paramType2,...). This utility method is primarily used internally to create unique identifiers for methods.

        Example Usage

        
         String signature = MethodUtils.buildSignature(String.class, "substring", new Class[]{int.class, int.class});
         System.out.println(signature);  // Output: java.lang.String#substring(int,int)
         
        
         String signature = MethodUtils.buildSignature(List.class, "add", new Class[]{Object.class});
         System.out.println(signature);  // Output: java.util.List#add(java.lang.Object)
         
        Parameters:
        declaringClass - the class that declares the method
        methodName - the name of the method
        parameterTypes - the parameter types of the method
        Returns:
        a non-null string representing the method signature
      • isObjectMethod

        public static boolean isObjectMethod​(java.lang.reflect.Method method)
        Checks whether the given method is declared by the Object class.

        This utility method helps determine if a method belongs directly to the root class Object, which is useful when filtering out methods that are common to all Java objects and not specific to a subclass.

        Example Usage

        
         Method toStringMethod = String.class.getMethod("toString");
         boolean isObjectMethod = MethodUtils.isObjectMethod(toStringMethod);
         System.out.println(isObjectMethod); // Output: true
         
        
         Method customMethod = MyClass.class.getMethod("customMethod");
         boolean isObjectMethod = MethodUtils.isObjectMethod(customMethod);
         System.out.println(isObjectMethod); // Output: false (assuming customMethod is defined in MyClass)
         
        Parameters:
        method - the method to check, may be null
        Returns:
        true if the method is declared by the Object class; false otherwise or if the method is null
      • isCallerSensitiveMethod

        public static boolean isCallerSensitiveMethod​(java.lang.reflect.Method method)
        Checks if the specified method is annotated with jdk.internal.reflect.CallerSensitive.

        The CallerSensitive annotation indicates that the method's behavior may be influenced by the caller's context. This is typically used in internal Java APIs to restrict or alter behavior based on the calling class.

        Example Usage

        
         Method defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);
         boolean isCallerSensitive = MethodUtils.isCallerSensitiveMethod(defineClassMethod);
         System.out.println("Is defineClass method caller-sensitive? " + isCallerSensitive);  // Likely output: true
         
        Parameters:
        method - the method to check, may be null
        Returns:
        true if the method is non-null and annotated with jdk.internal.reflect.CallerSensitive; false otherwise
        See Also:
        jdk.internal.reflect.CallerSensitive