public class MethodScanner extends Object
方法查找工具类,用于从指定的类或类层级结构中,根据特定规则搜索方法。
查找方式与查找范围
支持四种语义的查找:
Class.getMethods()的范围中查找匹配的方法;Class.getDeclaredMethods()的范围中查找匹配的方法;Class.getMethods()中所有匹配条件的方法,则应当调用findFromMethods(java.lang.Class<?>, org.dromara.hutool.core.reflect.method.MethodMetadataLookup<?>),
若我们希望获得类所有方法范围中首个匹配的方法,则应当调用getFromAllMethods(java.lang.Class<?>, org.dromara.hutool.core.reflect.method.MethodMetadataLookup<?>)。
匹配规则
方法查找的规则由MethodMetadataLookup实现。
规定,当MethodMetadataLookup.inspect(Method)方法返回元数据不为null时,则认为方法与其匹配,返回结果时将同时返回匹配的方法与元数据。
因此,我们可以通过实现MethodMetadataLookup接口来同时实现方法的查找与元数据的获取:
比如,我们希望查找所有方法上带有Annotation注解的方法,则可以实现如下:
Map<Method, Annotation> methods = MethodScanner.findFromAllMethods(Foo.class, method -> method.getAnnotation(Annotation.class));
此外,对于一些无需获取元数据的查找,我们可以使用MethodMatcherUtil提供的一些内置实现:
// 查找所有静态公开方法
Set<Method> methods = MethodScanner.findFromAllMethods(Foo.class, MethodMatcherUtils.isPublicStatic());
// 按照方法名与参数类型查找方法
Method method = MethodScanner.getFromAllMethods(Foo.class, MethodMatcherUtils.forNameAndParameterTypes("foo", String.class));
缓存
对于getDeclaredMethods(java.lang.Class<?>)与getMethods(java.lang.Class<?>)方法与基于这两个方法实现的,
所有xxxFromMethods与xxxFromDeclaredMethods方法,
都提供了缓存基于WeakConcurrentMap的缓存支持。
getAllMethods(java.lang.Class<?>)与所有xxxFromAllMethods方法都基于getDeclaredMethods(java.lang.Class<?>)实现,
但是每次全量查找,都需要重新遍历类层级结构,因此会带来一定的额外的性能损耗。
缓存在GC时会被回收,但是也可以通过clearCaches()手动清除缓存。
MethodMetadataLookup,
MethodMatcher,
MethodMatcherUtil| 构造器和说明 |
|---|
MethodScanner() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
clearCaches()
清空缓存
|
static Set<Method> |
findFromAllMethods(Class<?> type,
MethodMetadataLookup<?> lookup)
获取方法上带有指定元数据的方法
|
static Set<Method> |
findFromDeclaredMethods(Class<?> type,
MethodMetadataLookup<?> lookup)
获取方法上带有指定元数据的方法
|
static Set<Method> |
findFromMethods(Class<?> type,
MethodMetadataLookup<?> lookup)
获取方法上带有指定元数据的方法
|
static Set<Method> |
findFromSpecificMethods(Method[] methods,
MethodMetadataLookup<?> lookup)
从指定方法列表中筛选所有方法上带有指定元数据方法的方法
|
static <T> Map<Method,T> |
findWithMetadataFromAllMethods(Class<?> type,
MethodMetadataLookup<T> lookup)
获取方法上带有指定元数据的方法与对应元数据
|
static <T> Map<Method,T> |
findWithMetadataFromDeclaredMethods(Class<?> type,
MethodMetadataLookup<T> lookup)
获取方法上带有指定元数据的方法与对应元数据
|
static <T> Map<Method,T> |
findWithMetadataFromMethods(Class<?> type,
MethodMetadataLookup<T> lookup)
获取方法上带有指定元数据的方法与对应元数据
|
static <T> Map<Method,T> |
findWithMetadataFromSpecificMethods(Method[] methods,
MethodMetadataLookup<T> lookup)
从指定方法列表中筛选所有方法上带有指定元数据方法的方法与对应元数据
|
static Method[] |
getAllMethods(Class<?> type)
获取当前类层级结构中的所有方法。
|
static Method[] |
getDeclaredMethods(Class<?> type)
获取当前类直接声明的所有方法,等同于
Class.getDeclaredMethods() |
static Method |
getFromAllMethods(Class<?> type,
MethodMetadataLookup<?> lookup)
获取首个方法上带有指定元数据的方法及元数据
|
static Method |
getFromDeclaredMethods(Class<?> type,
MethodMetadataLookup<?> lookup)
获取首个方法上带有指定元数据的方法及元数据
|
static Method |
getFromMethods(Class<?> type,
MethodMetadataLookup<?> lookup)
获取首个方法上带有指定元数据的方法及元数据
|
static Method |
getFromSpecificMethods(Method[] methods,
MethodMetadataLookup<?> lookup)
从指定方法列表中筛选所有方法上带有指定元数据方法的方法
|
static Method[] |
getMethods(Class<?> type)
获取当前类及父类的所有公共方法,等同于
Class.getMethods() |
static <T> Map.Entry<Method,T> |
getWithMetadataFromAllMethods(Class<?> type,
MethodMetadataLookup<T> lookup)
获取首个方法上带有指定元数据的方法及元数据
|
static <T> Map.Entry<Method,T> |
getWithMetadataFromDeclaredMethods(Class<?> type,
MethodMetadataLookup<T> lookup)
获取首个方法上带有指定元数据的方法及元数据
|
static <T> Map.Entry<Method,T> |
getWithMetadataFromMethods(Class<?> type,
MethodMetadataLookup<T> lookup)
获取首个方法上带有指定元数据的方法及元数据
|
static <T> Map.Entry<Method,T> |
getWithMetadataFromSpecificMethods(Method[] methods,
MethodMetadataLookup<T> lookup)
从指定方法列表中筛选所有方法上带有指定元数据方法的方法与对应元数据
|
public static Method[] getMethods(Class<?> type)
Class.getMethods()type - 类public static Method[] getDeclaredMethods(Class<?> type)
Class.getDeclaredMethods()type - 类public static Method[] getAllMethods(Class<?> type)
获取当前类层级结构中的所有方法。
等同于按广度优先遍历类及其所有父类与接口,并依次调用Class.getDeclaredMethods()。
返回的方法排序规则如下:
type距离越近,则顺序越靠前;type距离相同,则父类优先于接口;type距离相同的接口,则顺序遵循接口在Class.getInterfaces()的顺序;type - 类ClassUtil.traverseTypeHierarchyWhile(Class, Predicate)public static void clearCaches()
public static <T> Map<Method,T> findWithMetadataFromSpecificMethods(Method[] methods, MethodMetadataLookup<T> lookup)
T - 结果类型methods - 方法列表lookup - 查找器public static Set<Method> findFromSpecificMethods(Method[] methods, MethodMetadataLookup<?> lookup)
methods - 方法列表lookup - 查找器public static <T> Map.Entry<Method,T> getWithMetadataFromSpecificMethods(Method[] methods, MethodMetadataLookup<T> lookup)
T - 值类型methods - 方法列表lookup - 查找器public static Method getFromSpecificMethods(Method[] methods, MethodMetadataLookup<?> lookup)
methods - 方法列表lookup - 查找器public static <T> Map<Method,T> findWithMetadataFromMethods(Class<?> type, MethodMetadataLookup<T> lookup)
T - 值类型type - 类型lookup - 查找器public static Set<Method> findFromMethods(Class<?> type, MethodMetadataLookup<?> lookup)
type - 类型lookup - 查找器public static <T> Map.Entry<Method,T> getWithMetadataFromMethods(Class<?> type, MethodMetadataLookup<T> lookup)
T - 值类型type - 类型lookup - 查找器nullpublic static Method getFromMethods(Class<?> type, MethodMetadataLookup<?> lookup)
type - 类型lookup - 查找器nullpublic static <T> Map<Method,T> findWithMetadataFromDeclaredMethods(Class<?> type, MethodMetadataLookup<T> lookup)
T - 值类型type - 类型lookup - 查找器public static Set<Method> findFromDeclaredMethods(Class<?> type, MethodMetadataLookup<?> lookup)
type - 类型lookup - 查找器public static <T> Map.Entry<Method,T> getWithMetadataFromDeclaredMethods(Class<?> type, MethodMetadataLookup<T> lookup)
T - 值类型type - 类型lookup - 查找器nullpublic static Method getFromDeclaredMethods(Class<?> type, MethodMetadataLookup<?> lookup)
type - 类型lookup - 查找器nullpublic static <T> Map<Method,T> findWithMetadataFromAllMethods(Class<?> type, MethodMetadataLookup<T> lookup)
T - 值类型type - 类型lookup - 查找器public static Set<Method> findFromAllMethods(Class<?> type, MethodMetadataLookup<?> lookup)
type - 类型lookup - 查找器public static <T> Map.Entry<Method,T> getWithMetadataFromAllMethods(Class<?> type, MethodMetadataLookup<T> lookup)
T - 值类型type - 类型lookup - 查找器nullpublic static Method getFromAllMethods(Class<?> type, MethodMetadataLookup<?> lookup)
type - 类型lookup - 查找器nullCopyright © 2023. All rights reserved.