Class MethodUtils


  • public abstract class MethodUtils
    extends BaseUtils
    The Java Reflection Method Utility class
    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Constructor Description
      MethodUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.function.Predicate<? super java.lang.reflect.Method> excludedDeclaredClass​(java.lang.Class<?> declaredClass)
      Create an instance of Predicate for Method to exclude the specified declared class
      static java.util.List<java.lang.reflect.Method> filterMethods​(java.lang.Class<?> targetClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Filter all methods of the target class by the specified Predicate
      static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Find the declared Method by the specified type (including inherited types) and method name and parameter types
      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) and method name and parameter types with cache
      static java.lang.reflect.Method findNearestOverriddenMethod​(java.lang.reflect.Method overrider)
      Find the nearest overridden method from the inherited class
      static java.lang.reflect.Method findOverriddenMethod​(java.lang.reflect.Method overrider, java.lang.Class<?> targetClass)
      Find the overridden method from the target class
      static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> targetClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      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, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      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, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      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, 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.String getSignature​(java.lang.reflect.Method method)
      Get the signature of the specified method
      static <R> R invokeMethod​(java.lang.Object instance, java.lang.Class<?> type, java.lang.String methodName, java.lang.Object... 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)
      Invoke the target objects' method
      static <R> R invokeStaticMethod​(java.lang.Class<?> targetClass, java.lang.String methodName, java.lang.Object... arguments)
      Invoke the target classes' static method
      static <R> R invokeStaticMethod​(java.lang.reflect.Method method, java.lang.Object... arguments)
      Invoke the target classes' static method
      static boolean isCallerSensitiveMethod​(java.lang.reflect.Method method)
      Test whether the specified method annotates jdk.internal.reflect.CallerSensitive or not
      static boolean isObjectMethod​(java.lang.reflect.Method method)  
      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

      • OBJECT_PUBLIC_METHODS

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

        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)
      • PULIC_METHOD_PREDICATE

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

      • MethodUtils

        public MethodUtils()
    • Method Detail

      • excludedDeclaredClass

        public static java.util.function.Predicate<? super java.lang.reflect.Method> excludedDeclaredClass​(java.lang.Class<?> declaredClass)
        Create an instance of Predicate for Method to exclude the specified declared class
        Parameters:
        declaredClass - the target class to exclude
        Returns:
        non-null
      • filterMethods

        public static java.util.List<java.lang.reflect.Method> filterMethods​(java.lang.Class<?> targetClass,
                                                                             boolean includeInheritedTypes,
                                                                             boolean publicOnly,
                                                                             java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Filter all methods of the target class by the specified Predicate
        Parameters:
        targetClass - the target class
        includeInheritedTypes - include the inherited types, e,g. super classes or interfaces
        publicOnly - only public method
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
      • getDeclaredMethods

        public static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> targetClass,
                                                                                  java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Get all declared methods of the target class, excluding the inherited methods
        Parameters:
        targetClass - the target class
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
        See Also:
        filterMethods(Class, boolean, boolean, Predicate[])
      • getMethods

        public static java.util.List<java.lang.reflect.Method> getMethods​(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.
        Parameters:
        targetClass - the target class
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
        See Also:
        filterMethods(Class, boolean, boolean, Predicate[])
      • getAllDeclaredMethods

        public static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> targetClass,
                                                                                     java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Get all declared methods of the target class, including the inherited methods.
        Parameters:
        targetClass - the target class
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
        See Also:
        filterMethods(Class, boolean, boolean, Predicate[])
      • getAllMethods

        public static java.util.List<java.lang.reflect.Method> getAllMethods​(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.
        Parameters:
        targetClass - the target class
        methodsToFilter - (optional) the methods to be filtered
        Returns:
        non-null read-only List
        See Also:
        filterMethods(Class, boolean, boolean, Predicate[])
      • findMethod

        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.
        Parameters:
        targetClass - the target type
        methodName - the specified method name
        Returns:
        if not found, return null
      • findMethod

        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) and method name and parameter types with cache
        Parameters:
        targetClass - the target type
        methodName - the method name
        parameterTypes - the parameter types
        Returns:
        if not found, return null
      • findDeclaredMethod

        public static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class<?> targetClass,
                                                                  java.lang.String methodName,
                                                                  java.lang.Class<?>... parameterTypes)
        Find the declared Method by the specified type (including inherited types) and method name and parameter types
        Parameters:
        targetClass - the target type
        methodName - the method name
        parameterTypes - the parameter types
        Returns:
      • invokeMethod

        public static <R> R invokeMethod​(java.lang.Object object,
                                         java.lang.String methodName,
                                         java.lang.Object... arguments)
        Invoke the target objects' method
        Type Parameters:
        R - the return type
        Parameters:
        object - the target object
        methodName - the method name
        arguments - the method arguments
        Returns:
        the target method's execution result
      • invokeStaticMethod

        public static <R> R invokeStaticMethod​(java.lang.Class<?> targetClass,
                                               java.lang.String methodName,
                                               java.lang.Object... arguments)
        Invoke the target classes' static method
        Type Parameters:
        R - the return type
        Parameters:
        targetClass - the target class
        methodName - the method name
        arguments - the method arguments
        Returns:
        the target method's execution result
      • invokeStaticMethod

        public static <R> R invokeStaticMethod​(java.lang.reflect.Method method,
                                               java.lang.Object... arguments)
        Invoke the target classes' static method
        Type Parameters:
        R - the return type
        Parameters:
        method - the method
        arguments - the method arguments
        Returns:
        the target method's execution result
      • invokeMethod

        public static <R> R invokeMethod​(java.lang.Object instance,
                                         java.lang.Class<?> type,
                                         java.lang.String methodName,
                                         java.lang.Object... arguments)
      • invokeMethod

        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.

        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.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.
        Parameters:
        overrider - the first method, possible overrider
        overridden - the second method, possibly being overridden
        Returns:
        true if and only if the first method overrides the second
        See Also:
        Elements.overrides(ExecutableElement, ExecutableElement, TypeElement)
      • findNearestOverriddenMethod

        public static java.lang.reflect.Method findNearestOverriddenMethod​(java.lang.reflect.Method overrider)
        Find the nearest overridden method from the inherited class
        Parameters:
        overrider - the overrider method
        Returns:
        if found, the overrider method, or null
      • findOverriddenMethod

        public static java.lang.reflect.Method findOverriddenMethod​(java.lang.reflect.Method overrider,
                                                                    java.lang.Class<?> targetClass)
        Find the overridden method from the target class
        Parameters:
        overrider - the overrider method
        targetClass - the class that is declaring the overridden method
        Returns:
        if found, the overrider method, or null
      • getSignature

        public static java.lang.String getSignature​(java.lang.reflect.Method method)
        Get the signature of the specified method
        Parameters:
        method - the specified method
        Returns:
        non-null
      • isObjectMethod

        public static boolean isObjectMethod​(java.lang.reflect.Method method)
      • isCallerSensitiveMethod

        public static boolean isCallerSensitiveMethod​(java.lang.reflect.Method method)
        Test whether the specified method annotates jdk.internal.reflect.CallerSensitive or not
        Parameters:
        method - Method
        Returns:
        true if the specified method annotates jdk.internal.reflect.CallerSensitive
        See Also:
        jdk.internal.reflect.CallerSensitive