类 MethodUtils


  • public abstract class MethodUtils
    extends BaseUtils
    The Java Reflection Method Utility class
    从以下版本开始:
    1.0.0
    作者:
    Mercy
    • 构造器概要

      构造器 
      构造器 说明
      MethodUtils()  
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static java.util.function.Predicate<java.lang.reflect.Method> excludedDeclaredClass​(java.lang.Class<?> declaredClass)
      Create an instance of Predicate for Method to exclude the specified declared class
      static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class<?> declaredClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)  
      static java.lang.reflect.Method findMethod​(java.lang.Class type, java.lang.String methodName)
      Find the Method by the the specified type(including inherited types) and method name without the parameter type.
      static java.lang.reflect.Method findMethod​(java.lang.Class type, java.lang.String methodName, java.lang.Class<?>... parameterTypes)
      Find the Method by the the specified type(including inherited types) and method name and parameter types
      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<?> declaringClass)
      Find the overridden method from the declaring class
      static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> declaringClass, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Get all declared methods of the declared class, including the inherited methods.
      static java.util.List<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> declaringClass, java.util.function.Predicate<java.lang.reflect.Method>... methodsToFilter)
      Get all public methods of the declared class, including the inherited methods.
      static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> declaredClass, java.lang.String methodName, java.lang.Class<?>... parameterTypes)  
      static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> declaringClass, java.util.function.Predicate<java.lang.reflect.Method>... methodsToFilter)
      Get all declared methods of the declared class, excluding the inherited methods
      static java.util.List<java.lang.reflect.Method> getMethods​(java.lang.Class<?> declaringClass, boolean includeInheritedTypes, boolean publicOnly, java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
      Get all methods of the declared class
      static java.util.List<java.lang.reflect.Method> getMethods​(java.lang.Class<?> declaringClass, java.util.function.Predicate<java.lang.reflect.Method>... methodsToFilter)
      Get all public methods of the declared 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<?> type, 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.
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 字段详细资料

      • OBJECT_METHOD_PREDICATE

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

        public static final java.util.Set<java.lang.reflect.Method> OBJECT_METHODS
    • 构造器详细资料

      • MethodUtils

        public MethodUtils()
    • 方法详细资料

      • excludedDeclaredClass

        public static java.util.function.Predicate<java.lang.reflect.Method> excludedDeclaredClass​(java.lang.Class<?> declaredClass)
        Create an instance of Predicate for Method to exclude the specified declared class
        参数:
        declaredClass - the declared class to exclude
        返回:
        non-null
      • getMethods

        public static java.util.List<java.lang.reflect.Method> getMethods​(java.lang.Class<?> declaringClass,
                                                                          boolean includeInheritedTypes,
                                                                          boolean publicOnly,
                                                                          java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Get all methods of the declared class
        参数:
        declaringClass - the declared class
        includeInheritedTypes - include the inherited types, e,g. super classes or interfaces
        publicOnly - only public method
        methodsToFilter - (optional) the methods to be filtered
        返回:
        non-null read-only List
      • getDeclaredMethods

        public static java.util.List<java.lang.reflect.Method> getDeclaredMethods​(java.lang.Class<?> declaringClass,
                                                                                  java.util.function.Predicate<java.lang.reflect.Method>... methodsToFilter)
        Get all declared methods of the declared class, excluding the inherited methods
        参数:
        declaringClass - the declared class
        methodsToFilter - (optional) the methods to be filtered
        返回:
        non-null read-only List
        另请参阅:
        getMethods(Class, boolean, boolean, Predicate[])
      • getMethods

        public static java.util.List<java.lang.reflect.Method> getMethods​(java.lang.Class<?> declaringClass,
                                                                          java.util.function.Predicate<java.lang.reflect.Method>... methodsToFilter)
        Get all public methods of the declared class, including the inherited methods.
        参数:
        declaringClass - the declared class
        methodsToFilter - (optional) the methods to be filtered
        返回:
        non-null read-only List
        另请参阅:
        getMethods(Class, boolean, boolean, Predicate[])
      • getAllDeclaredMethods

        public static java.util.List<java.lang.reflect.Method> getAllDeclaredMethods​(java.lang.Class<?> declaringClass,
                                                                                     java.util.function.Predicate<? super java.lang.reflect.Method>... methodsToFilter)
        Get all declared methods of the declared class, including the inherited methods.
        参数:
        declaringClass - the declared class
        methodsToFilter - (optional) the methods to be filtered
        返回:
        non-null read-only List
        另请参阅:
        getMethods(Class, boolean, boolean, Predicate[])
      • getAllMethods

        public static java.util.List<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> declaringClass,
                                                                             java.util.function.Predicate<java.lang.reflect.Method>... methodsToFilter)
        Get all public methods of the declared class, including the inherited methods.
        参数:
        declaringClass - the declared class
        methodsToFilter - (optional) the methods to be filtered
        返回:
        non-null read-only List
        另请参阅:
        getMethods(Class, boolean, boolean, Predicate[])
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class type,
                                                          java.lang.String methodName)
        Find the Method by the the specified type(including inherited types) and method name without the parameter type.
        参数:
        type - the target type
        methodName - the specified method name
        返回:
        if not found, return null
      • findMethod

        public static java.lang.reflect.Method findMethod​(java.lang.Class type,
                                                          java.lang.String methodName,
                                                          java.lang.Class<?>... parameterTypes)
        Find the Method by the the specified type(including inherited types) and method name and parameter types
        参数:
        type - the target type
        methodName - the method name
        parameterTypes - the parameter types
        返回:
        if not found, return null
      • findDeclaredMethod

        public static java.lang.reflect.Method findDeclaredMethod​(java.lang.Class<?> declaredClass,
                                                                  java.lang.String methodName,
                                                                  java.lang.Class<?>... parameterTypes)
      • getDeclaredMethod

        public static java.lang.reflect.Method getDeclaredMethod​(java.lang.Class<?> declaredClass,
                                                                 java.lang.String methodName,
                                                                 java.lang.Class<?>... parameterTypes)
      • invokeMethod

        public static <R> R invokeMethod​(java.lang.Object object,
                                         java.lang.String methodName,
                                         java.lang.Object... arguments)
        Invoke the target objects' method
        类型参数:
        R - the return type
        参数:
        object - the target object
        methodName - the method name
        arguments - the method arguments
        返回:
        the target method's execution result
      • invokeStaticMethod

        public static <R> R invokeStaticMethod​(java.lang.Class<?> type,
                                               java.lang.String methodName,
                                               java.lang.Object... arguments)
        Invoke the target classes' static method
        类型参数:
        R - the return type
        参数:
        type - the target class
        methodName - the method name
        arguments - the method arguments
        返回:
        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
        类型参数:
        R - the return type
        参数:
        method - the method
        arguments - the method arguments
        返回:
        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.

        类型参数:
        R -
        参数:
        instance - the object the underlying method is invoked from
        method - the underlying method
        arguments - the arguments used for the method call
        返回:
        the result of dispatching the method represented by this object on instance with parameters arguments
        抛出:
        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.
        参数:
        overrider - the first method, possible overrider
        overridden - the second method, possibly being overridden
        返回:
        true if and only if the first method overrides the second
        另请参阅:
        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
        参数:
        overrider - the overrider method
        返回:
        if found, the overrider method, or null
      • findOverriddenMethod

        public static java.lang.reflect.Method findOverriddenMethod​(java.lang.reflect.Method overrider,
                                                                    java.lang.Class<?> declaringClass)
        Find the overridden method from the declaring class
        参数:
        overrider - the overrider method
        declaringClass - the class that is declaring the overridden method
        返回:
        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
        参数:
        method - the specified method
        返回:
        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
        参数:
        method - Method
        返回:
        true if the specified method annotates jdk.internal.reflect.CallerSensitive
        另请参阅:
        jdk.internal.reflect.CallerSensitive