public class MethodMatcherUtil extends Object
| Constructor and Description | 
|---|
| MethodMatcherUtil() | 
| Modifier and Type | Method and Description | 
|---|---|
| static Predicate<Method> | allMatch(Predicate<Method>... matchers)用于组合多个方法匹配器的方法匹配器,当所有方法匹配器均匹配成功时才认为方法匹配。 | 
| static Predicate<Method> | anyMatch(Predicate<Method>... matchers)用于组合多个方法匹配器的方法匹配器,当任意方法匹配器匹配成功时即认为方法匹配。 | 
| static Predicate<Method> | forGetterMethod(Field field)用于获得指定属性的getter方法的匹配器
 
     查找方法名为 get + 首字母大写的属性名的无参数方法;
     查找方法名为属性名的无参数方法;
     若fieldType为boolean或Boolean,则同时查找方法名为is + 首字母大写的属性的无参数方法; | 
| static Predicate<Method> | forGetterMethod(String fieldName,
               Class<?> fieldType)用于获得指定属性的getter方法的匹配器
 
     查找方法名为 get + 首字母大写的属性名的无参数方法;
     查找方法名为属性名的无参数方法;
     若fieldType为boolean或Boolean,则同时查找方法名为is + 首字母大写的属性的无参数方法; | 
| static Predicate<Method> | forMethodSignature(Method method)用于匹配方法签名的方法匹配器,检查的内容包括:
 
     方法名是否完全一致;
     返回值类型是否匹配,允许返回值类型为方法返回值类型的子类;
     参数类型是否匹配,允许参数类型为方法参数类型的子类;
  | 
| static Predicate<Method> | forMethodSignature(String methodName,
                  Class<?> returnType,
                  Class<?>... parameterTypes)用于匹配方法签名的方法匹配器,检查的内容包括:
 
     方法名是否完全一致;
     返回值类型是否匹配,允许返回值类型为方法返回值类型的子类,若返回值类型为 null则表示匹配无返回值的方法;
     参数类型是否匹配,允许参数类型为方法参数类型的子类,若参数类型为null则表示匹配无参数的方法; | 
| static Predicate<Method> | forModifiers(int... modifiers)用于具有指定修饰符的方法的方法匹配器。 | 
| static Predicate<Method> | forMostSpecificParameterTypes(Class<?>... parameterTypes)用于匹配指定参数类型的方法的方法匹配器,与 forParameterTypes(java.lang.Class<?>...)不同的是,该方法仅用于尽量可能最匹配的方法
 
     若参数为空,则表示匹配无参数方法;
     
         若参数不为空:
         
             仅匹配parameterTypes中不为null的参数类型,若参数类型为null则表示匹配任意类型的参数;
             若N为parameterTypes长度,则仅要求parameterTypes不为null的类型与方法前N个参数类型匹配即可;
             若parameterTypes长度大于参数列表长度,则直接返回false;
         
     
 
 比如:若存在三参数方法 method(String, Integer, Object),支持以下匹配:forMostSpecificParameterTypes(CharSequence.class, Number.class, Object.class)forMostSpecificParameterTypes(String.class, Integer.class, Object.class)forMostSpecificParameterTypes(String.class, Integer.class, null)forMostSpecificParameterTypes(String.class, null, null)forMostSpecificParameterTypes(null, null, null)forMostSpecificParameterTypes(String.class, Integer.class)forMostSpecificParameterTypes(String.class) | 
| static Predicate<Method> | forMostSpecificStrictParameterTypes(Class<?>... parameterTypes)用于匹配指定参数类型的方法的方法匹配器,与 forParameterTypes(java.lang.Class<?>...)不同的是,该方法仅用于尽量可能最匹配的方法
 
     若参数为空,则表示匹配无参数方法;
     
         若参数不为空:
         
             仅匹配parameterTypes中不为null的参数类型,若参数类型为null则表示匹配任意类型的参数;
             若N为parameterTypes长度,则仅要求parameterTypes不为null的类型与方法前N个参数类型匹配即可;
             若parameterTypes长度大于参数列表长度,则直接返回false;
         
     
 
 比如:若存在三参数方法 method(String, Integer, Object),支持以下匹配:forMostSpecificParameterTypes(String.class, Integer.class, Object.class)forMostSpecificParameterTypes(String.class, Integer.class, null)forMostSpecificParameterTypes(String.class, null, null)forMostSpecificParameterTypes(null, null, null)forMostSpecificParameterTypes(String.class, Integer.class)forMostSpecificParameterTypes(String.class) | 
| static Predicate<Method> | forName(String methodName)用于根据方法名匹配方法的方法匹配器。 | 
| static Predicate<Method> | forNameAndParameterTypes(String methodName,
                        Class<?>... parameterTypes)用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时允许参数类型为方法参数类型的子类。 | 
| static Predicate<Method> | forNameAndStrictParameterTypes(String methodName,
                              Class<?>... parameterTypes)用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时要求参数类型与方法参数类型完全一致。 | 
| static Predicate<Method> | forNameIgnoreCase(String methodName)用于根据方法名匹配方法的方法匹配器,忽略方法名大小写。 | 
| static Predicate<Method> | forNameIgnoreCaseAndParameterTypes(String methodName,
                                  Class<?>... parameterTypes)用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时允许参数类型为方法参数类型的子类,且方法名忽略大小写。 | 
| static Predicate<Method> | forNameIgnoreCaseAndStrictParameterTypes(String methodName,
                                        Class<?>... parameterTypes)用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时要求参数类型与方法参数类型完全一致,且方法名忽略大小写。 | 
| static Predicate<Method> | forNoneParameter()用于匹配无参数方法的方法匹配器。 | 
| static Predicate<Method> | forNoneReturnType()用于匹配无返回值的方法的方法匹配器。 | 
| static Predicate<Method> | forParameterCount(int count)用于匹配指定参数个数的方法的方法匹配器。 | 
| static Predicate<Method> | forParameterTypes(Class<?>... parameterTypes)用于匹配指定参数类型的方法的方法匹配器,只要参数类型可以赋值给方法参数类型即认为匹配成功。 | 
| static Predicate<Method> | forReturnType(Class<?> returnType)用于匹配指定参数类型的方法的方法匹配器,只要参数类型可以赋值给方法参数类型。 | 
| static Predicate<Method> | forSetterMethod(Field field)用于获得指定属性的setter方法的匹配器,默认查找方法名为 set + 首字母大写的属性的单参数方法。 | 
| static Predicate<Method> | forSetterMethod(String fieldName,
               Class<?> fieldType)用于获得指定属性的setter方法的匹配器,默认查找方法名为 set + 首字母大写的属性的单参数方法。 | 
| static Predicate<Method> | forStrictMethodSignature(Method method)用于匹配方法签名的方法匹配器,检查的内容包括:
 
     方法名是否完全一致;
     返回值类型是否匹配,要求返回值类型与方法返回值类型完全一致;
     参数类型是否匹配,要求参数类型与方法参数类型完全一致;
  | 
| static Predicate<Method> | forStrictMethodSignature(String methodName,
                        Class<?> returnType,
                        Class<?>... parameterTypes)用于匹配方法签名的方法匹配器,检查的内容包括:
 
     方法名是否完全一致;
     返回值类型是否匹配,要求返回值类型与方法返回值类型完全一致,若返回值类型为 null则表示匹配无返回值的方法;
     参数类型是否匹配,要求参数类型与方法参数类型完全一致,若参数类型为null则表示匹配无参数的方法; | 
| static Predicate<Method> | forStrictParameterTypes(Class<?>... parameterTypes)用于匹配指定参数类型的方法的方法匹配器,只有参数类型完全匹配才认为匹配成功。 | 
| static Predicate<Method> | forStrictReturnType(Class<?> returnType)用于匹配指定返回值类型的方法的方法匹配器,要求返回值类型与指定类型完全一致。 | 
| static Predicate<Method> | hasAnnotation(Class<? extends Annotation> annotationType)用于匹配被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。 | 
| static Predicate<Method> | hasAnnotationOnDeclaringClass(Class<? extends Annotation> annotationType)用于匹配声明方法的类的层级接口中,存在任意类被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。 | 
| static Predicate<Method> | hasAnnotationOnMethodOrDeclaringClass(Class<? extends Annotation> annotationType)用于匹配方法本身或声明方法的类上,直接被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。 | 
| static Predicate<Method> | hasDeclaredAnnotation(Class<? extends Annotation> annotationType)用于匹配被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。 | 
| static Predicate<Method> | isPublic()用于匹配共有方法的方法匹配器。 | 
| static Predicate<Method> | isPublicStatic()用于匹配公共静态方法的方法匹配器。 | 
| static Predicate<Method> | isStatic()用于匹配静态方法的方法匹配器。 | 
| static Predicate<Method> | noneMatch(Predicate<Method>... matchers)用于组合多个方法匹配器的方法匹配器,仅当所有方法匹配器均匹配失败时才认为方法匹配。 | 
@SafeVarargs public static Predicate<Method> noneMatch(Predicate<Method>... matchers)
用于组合多个方法匹配器的方法匹配器,仅当所有方法匹配器均匹配失败时才认为方法匹配。
matchers - 方法匹配器Stream.noneMatch(java.util.function.Predicate<? super T>)@SafeVarargs public static Predicate<Method> anyMatch(Predicate<Method>... matchers)
用于组合多个方法匹配器的方法匹配器,当任意方法匹配器匹配成功时即认为方法匹配。
matchers - 方法匹配器Stream.anyMatch(java.util.function.Predicate<? super T>)@SafeVarargs public static Predicate<Method> allMatch(Predicate<Method>... matchers)
用于组合多个方法匹配器的方法匹配器,当所有方法匹配器均匹配成功时才认为方法匹配。
matchers - 方法匹配器Stream.allMatch(java.util.function.Predicate<? super T>)public static Predicate<Method> forModifiers(int... modifiers)
用于具有指定修饰符的方法的方法匹配器。
modifiers - 修饰符public static Predicate<Method> hasDeclaredAnnotation(Class<? extends Annotation> annotationType)
用于匹配被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。
 比如:指定注解为 @Annotation,则匹配直接被@Annotation标注的方法。
annotationType - 注解类型AnnotatedElementUtil.isAnnotationPresent(java.lang.reflect.AnnotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation>)public static Predicate<Method> hasAnnotation(Class<? extends Annotation> annotationType)
用于匹配被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。
 比如:指定注解为 @Annotation,则匹配:
 
@Annotation标注的方法;@Annotation注解的派生注解标注的方法;annotationType - 注解类型AnnotatedElementUtil.isAnnotationPresent(java.lang.reflect.AnnotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation>)public static Predicate<Method> hasAnnotationOnDeclaringClass(Class<? extends Annotation> annotationType)
用于匹配声明方法的类的层级接口中,存在任意类被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。
 比如:指定注解为 @Annotation,则匹配:
 
@Annotation标注的方法;@Annotation注解的派生注解标注的方法;annotationType - 注解类型AnnotatedElementUtil.isAnnotationPresent(java.lang.reflect.AnnotatedElement, java.lang.Class<? extends java.lang.annotation.Annotation>)public static Predicate<Method> hasAnnotationOnMethodOrDeclaringClass(Class<? extends Annotation> annotationType)
用于匹配方法本身或声明方法的类上,直接被指定注解标注、或注解层级结构中存在指定注解的方法的方法匹配器。
 比如:指定注解为 @Annotation,则匹配:
 
@Annotation标注的方法;@Annotation注解的派生注解标注的方法;@Annotation标注的方法;@Annotation注解的派生注解标注的方法;annotationType - 注解类型public static Predicate<Method> forGetterMethod(String fieldName, Class<?> fieldType)
用于获得指定属性的getter方法的匹配器
get + 首字母大写的属性名的无参数方法;fieldType为boolean或Boolean,则同时查找方法名为is + 首字母大写的属性的无参数方法;fieldName - 属性名fieldType - 属性类型public static Predicate<Method> forGetterMethod(Field field)
用于获得指定属性的getter方法的匹配器
get + 首字母大写的属性名的无参数方法;fieldType为boolean或Boolean,则同时查找方法名为is + 首字母大写的属性的无参数方法;field - 属性public static Predicate<Method> forSetterMethod(String fieldName, Class<?> fieldType)
用于获得指定属性的setter方法的匹配器,默认查找方法名为set + 首字母大写的属性的单参数方法。
 
set + 首字母大写的属性名的单参数方法;fieldName - 属性名fieldType - 属性类型public static Predicate<Method> forSetterMethod(Field field)
用于获得指定属性的setter方法的匹配器,默认查找方法名为set + 首字母大写的属性的单参数方法。
 
set + 首字母大写的属性名的单参数方法;field - 属性public static Predicate<Method> forNameAndParameterTypes(String methodName, Class<?>... parameterTypes)
用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时允许参数类型为方法参数类型的子类。
methodName - 方法名parameterTypes - 参数类型public static Predicate<Method> forNameAndStrictParameterTypes(String methodName, Class<?>... parameterTypes)
用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时要求参数类型与方法参数类型完全一致。
methodName - 方法名parameterTypes - 参数类型public static Predicate<Method> forNameIgnoreCaseAndParameterTypes(String methodName, Class<?>... parameterTypes)
用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时允许参数类型为方法参数类型的子类,且方法名忽略大小写。
methodName - 方法名parameterTypes - 参数类型public static Predicate<Method> forNameIgnoreCaseAndStrictParameterTypes(String methodName, Class<?>... parameterTypes)
用于同时匹配方法名和参数类型的方法匹配器,其中,参数类型匹配时要求参数类型与方法参数类型完全一致,且方法名忽略大小写。
methodName - 方法名parameterTypes - 参数类型public static Predicate<Method> forMethodSignature(Method method)
用于匹配方法签名的方法匹配器,检查的内容包括:
method - 方法public static Predicate<Method> forMethodSignature(String methodName, Class<?> returnType, Class<?>... parameterTypes)
用于匹配方法签名的方法匹配器,检查的内容包括:
null则表示匹配无返回值的方法;null则表示匹配无参数的方法;methodName - 方法名returnType - 返回值类型,若为null则表示匹配无返回值的方法parameterTypes - 参数类型,若为null则表示匹配无参数的方法public static Predicate<Method> forStrictMethodSignature(String methodName, Class<?> returnType, Class<?>... parameterTypes)
用于匹配方法签名的方法匹配器,检查的内容包括:
null则表示匹配无返回值的方法;null则表示匹配无参数的方法;methodName - 方法名returnType - 返回值类型,若为null则表示匹配无返回值的方法parameterTypes - 参数类型,若为null则表示匹配无参数的方法public static Predicate<Method> forStrictMethodSignature(Method method)
用于匹配方法签名的方法匹配器,检查的内容包括:
method - 方法public static Predicate<Method> forName(String methodName)
用于根据方法名匹配方法的方法匹配器。
methodName - 方法名public static Predicate<Method> forNameIgnoreCase(String methodName)
用于根据方法名匹配方法的方法匹配器,忽略方法名大小写。
methodName - 方法名public static Predicate<Method> forNoneReturnType()
用于匹配无返回值的方法的方法匹配器。
public static Predicate<Method> forReturnType(Class<?> returnType)
用于匹配指定参数类型的方法的方法匹配器,只要参数类型可以赋值给方法参数类型。
returnType - 返回值类型public static Predicate<Method> forStrictReturnType(Class<?> returnType)
用于匹配指定返回值类型的方法的方法匹配器,要求返回值类型与指定类型完全一致。
returnType - 返回值类型public static Predicate<Method> forParameterCount(int count)
用于匹配指定参数个数的方法的方法匹配器。
count - 参数个数public static Predicate<Method> forParameterTypes(Class<?>... parameterTypes)
用于匹配指定参数类型的方法的方法匹配器,只要参数类型可以赋值给方法参数类型即认为匹配成功。
 比如:参数类型为ArrayList,则方法参数类型可以为List、Collection等。
parameterTypes - 参数类型public static Predicate<Method> forMostSpecificParameterTypes(Class<?>... parameterTypes)
用于匹配指定参数类型的方法的方法匹配器,与forParameterTypes(java.lang.Class<?>...)不同的是,该方法仅用于尽量可能最匹配的方法
 
parameterTypes中不为null的参数类型,若参数类型为null则表示匹配任意类型的参数;parameterTypes长度,则仅要求parameterTypes不为null的类型与方法前N个参数类型匹配即可;parameterTypes长度大于参数列表长度,则直接返回false;method(String, Integer, Object),支持以下匹配:
 forMostSpecificParameterTypes(CharSequence.class, Number.class, Object.class)forMostSpecificParameterTypes(String.class, Integer.class, Object.class)forMostSpecificParameterTypes(String.class, Integer.class, null)forMostSpecificParameterTypes(String.class, null, null)forMostSpecificParameterTypes(null, null, null)forMostSpecificParameterTypes(String.class, Integer.class)forMostSpecificParameterTypes(String.class)parameterTypes - 参数类型public static Predicate<Method> forMostSpecificStrictParameterTypes(Class<?>... parameterTypes)
用于匹配指定参数类型的方法的方法匹配器,与forParameterTypes(java.lang.Class<?>...)不同的是,该方法仅用于尽量可能最匹配的方法
 
parameterTypes中不为null的参数类型,若参数类型为null则表示匹配任意类型的参数;parameterTypes长度,则仅要求parameterTypes不为null的类型与方法前N个参数类型匹配即可;parameterTypes长度大于参数列表长度,则直接返回false;method(String, Integer, Object),支持以下匹配:
 forMostSpecificParameterTypes(String.class, Integer.class, Object.class)forMostSpecificParameterTypes(String.class, Integer.class, null)forMostSpecificParameterTypes(String.class, null, null)forMostSpecificParameterTypes(null, null, null)forMostSpecificParameterTypes(String.class, Integer.class)forMostSpecificParameterTypes(String.class)parameterTypes - 参数类型Copyright © 2025. All rights reserved.