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