mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix
This commit is contained in:
parent
33197358f8
commit
9c4d71d36a
@ -17,24 +17,15 @@ import java.util.function.Predicate;
|
||||
@FunctionalInterface
|
||||
public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<Method> {
|
||||
|
||||
/**
|
||||
* 检查方法是否匹配
|
||||
*
|
||||
* @param method 方法
|
||||
* @return 结果
|
||||
*/
|
||||
boolean test(Method method);
|
||||
|
||||
/**
|
||||
* 返回一个组合的条件,当且仅当所有条件都符合时,才返回{@code true},
|
||||
* 等同于{@link Predicate#and(Predicate)}.
|
||||
*
|
||||
* @param other 其他条件
|
||||
* @return 条件
|
||||
* @throws NullPointerException 当other为null时抛出
|
||||
*/
|
||||
@Override
|
||||
default MethodMatcher and(Predicate<? super Method> other) {
|
||||
default MethodMatcher and(final Predicate<? super Method> other) {
|
||||
Objects.requireNonNull(other);
|
||||
return t -> test(t) && other.test(t);
|
||||
}
|
||||
@ -57,7 +48,7 @@ public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<
|
||||
* @throws NullPointerException 当other为null时抛出
|
||||
*/
|
||||
@Override
|
||||
default MethodMatcher or(Predicate<? super Method> other) {
|
||||
default MethodMatcher or(final Predicate<? super Method> other) {
|
||||
Objects.requireNonNull(other);
|
||||
return t -> test(t) || other.test(t);
|
||||
}
|
||||
@ -69,7 +60,7 @@ public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<
|
||||
* @return 结果
|
||||
*/
|
||||
@Override
|
||||
default Boolean inspect(Method method) {
|
||||
default Boolean inspect(final Method method) {
|
||||
return test(method) ? Boolean.TRUE : null;
|
||||
}
|
||||
}
|
||||
|
@ -318,7 +318,7 @@ public class MethodMatcherUtil {
|
||||
* @return 方法匹配器
|
||||
*/
|
||||
public static MethodMatcher forNameIgnoreCaseAndStrictParameterTypes(
|
||||
final String methodName, Class<?>... parameterTypes) {
|
||||
final String methodName, final Class<?>... parameterTypes) {
|
||||
Objects.requireNonNull(methodName);
|
||||
Objects.requireNonNull(parameterTypes);
|
||||
return allMatch(forNameIgnoreCase(methodName), forStrictParameterTypes(parameterTypes));
|
||||
@ -352,8 +352,8 @@ public class MethodMatcherUtil {
|
||||
* <li>参数类型是否匹配,允许参数类型为方法参数类型的子类,若参数类型为{@code null}则表示匹配无参数的方法;</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param methodName 方法名
|
||||
* @param returnType 返回值类型,若为{@code null}则表示匹配无返回值的方法
|
||||
* @param methodName 方法名
|
||||
* @param returnType 返回值类型,若为{@code null}则表示匹配无返回值的方法
|
||||
* @param parameterTypes 参数类型,若为{@code null}则表示匹配无参数的方法
|
||||
* @return 方法匹配器
|
||||
*/
|
||||
@ -375,8 +375,8 @@ public class MethodMatcherUtil {
|
||||
* <li>参数类型是否匹配,要求参数类型与方法参数类型完全一致,若参数类型为{@code null}则表示匹配无参数的方法;</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param methodName 方法名
|
||||
* @param returnType 返回值类型,若为{@code null}则表示匹配无返回值的方法
|
||||
* @param methodName 方法名
|
||||
* @param returnType 返回值类型,若为{@code null}则表示匹配无返回值的方法
|
||||
* @param parameterTypes 参数类型,若为{@code null}则表示匹配无参数的方法
|
||||
* @return 方法匹配器
|
||||
*/
|
||||
@ -474,7 +474,7 @@ public class MethodMatcherUtil {
|
||||
* @param count 参数个数
|
||||
* @return 方法匹配器
|
||||
*/
|
||||
public static MethodMatcher forParameterCount(int count) {
|
||||
public static MethodMatcher forParameterCount(final int count) {
|
||||
return method -> method.getParameterCount() == count;
|
||||
}
|
||||
|
||||
@ -568,7 +568,7 @@ public class MethodMatcherUtil {
|
||||
|
||||
@NotNull
|
||||
private static MethodMatcher mostSpecificStrictParameterTypesMatcher(
|
||||
Class<?>[] parameterTypes, BiPredicate<Class<?>, Class<?>> typeMatcher) {
|
||||
final Class<?>[] parameterTypes, final BiPredicate<Class<?>, Class<?>> typeMatcher) {
|
||||
Objects.requireNonNull(parameterTypes);
|
||||
// 若参数为空,则表示匹配无参数方法
|
||||
if (parameterTypes.length == 0) {
|
||||
|
@ -5,6 +5,7 @@ import java.lang.reflect.Method;
|
||||
/**
|
||||
* 方法的元数据查找器,参照 spring 的 {@code MethodIntrospector.MetadataLookup},用于从方法上获得特定的元数据。
|
||||
*
|
||||
* @param <T> 返回类型
|
||||
* @author huangchengxing
|
||||
* @see MethodMatcher
|
||||
* @see MethodMatcherUtil
|
||||
|
@ -63,8 +63,8 @@ import java.util.function.Predicate;
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class MethodScanner {
|
||||
/**
|
||||
* TODO 替换{@link MethodUtil}中的部分方法实现
|
||||
/*
|
||||
TODO 替换{@link MethodUtil}中的部分方法实现
|
||||
*/
|
||||
|
||||
/**
|
||||
@ -90,7 +90,7 @@ public class MethodScanner {
|
||||
* @param type 类
|
||||
* @return 当前类及父类的所有公共方法
|
||||
*/
|
||||
public static Method[] getMethods(Class<?> type) {
|
||||
public static Method[] getMethods(final Class<?> type) {
|
||||
if (Objects.isNull(type)) {
|
||||
return EMPTY_METHODS;
|
||||
}
|
||||
@ -103,7 +103,7 @@ public class MethodScanner {
|
||||
* @param type 类
|
||||
* @return 当前类及父类的所有公共方法
|
||||
*/
|
||||
public static Method[] getDeclaredMethods(Class<?> type) {
|
||||
public static Method[] getDeclaredMethods(final Class<?> type) {
|
||||
if (Objects.isNull(type)) {
|
||||
return EMPTY_METHODS;
|
||||
}
|
||||
@ -124,11 +124,11 @@ public class MethodScanner {
|
||||
* @return 当前类及父类的所有公共方法
|
||||
* @see ClassUtil#traverseTypeHierarchyWhile(Class, Predicate)
|
||||
*/
|
||||
public static Method[] getAllMethods(Class<?> type) {
|
||||
public static Method[] getAllMethods(final Class<?> type) {
|
||||
if (Objects.isNull(type)) {
|
||||
return EMPTY_METHODS;
|
||||
}
|
||||
List<Method> methods = new ArrayList<>();
|
||||
final List<Method> methods = new ArrayList<>();
|
||||
ClassUtil.traverseTypeHierarchyWhile(type, t -> {
|
||||
methods.addAll(Arrays.asList(getDeclaredMethods(t)));
|
||||
return true;
|
||||
@ -154,14 +154,15 @@ public class MethodScanner {
|
||||
* @param methods 方法列表
|
||||
* @param lookup 查找器
|
||||
* @return 方法与对应的元数据集合
|
||||
* @param <T> 结果类型
|
||||
*/
|
||||
public static <T> Map<Method, T> findWithMetadataFromSpecificMethods(Method[] methods, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map<Method, T> findWithMetadataFromSpecificMethods(final Method[] methods, final MethodMetadataLookup<T> lookup) {
|
||||
if (ArrayUtil.isEmpty(methods)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<Method, T> results = new LinkedHashMap<>();
|
||||
for (Method method : methods) {
|
||||
T result = lookup.inspect(method);
|
||||
final Map<Method, T> results = new LinkedHashMap<>();
|
||||
for (final Method method : methods) {
|
||||
final T result = lookup.inspect(method);
|
||||
if (Objects.nonNull(result)) {
|
||||
results.put(method, result);
|
||||
}
|
||||
@ -176,7 +177,7 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法集合
|
||||
*/
|
||||
public static Set<Method> findFromSpecificMethods(Method[] methods, MethodMetadataLookup<?> lookup) {
|
||||
public static Set<Method> findFromSpecificMethods(final Method[] methods, final MethodMetadataLookup<?> lookup) {
|
||||
return findWithMetadataFromSpecificMethods(methods, lookup).keySet();
|
||||
}
|
||||
|
||||
@ -186,10 +187,11 @@ public class MethodScanner {
|
||||
* @param methods 方法列表
|
||||
* @param lookup 查找器
|
||||
* @return 方法与对应的元数据
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromSpecificMethods(Method[] methods, MethodMetadataLookup<T> lookup) {
|
||||
for (Method method : methods) {
|
||||
T result = lookup.inspect(method);
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromSpecificMethods(final Method[] methods, final MethodMetadataLookup<T> lookup) {
|
||||
for (final Method method : methods) {
|
||||
final T result = lookup.inspect(method);
|
||||
if (Objects.nonNull(result)) {
|
||||
return MapUtil.entry(method, result);
|
||||
}
|
||||
@ -204,8 +206,8 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法
|
||||
*/
|
||||
public static Method getFromSpecificMethods(Method[] methods, MethodMetadataLookup<?> lookup) {
|
||||
Map.Entry<Method, ?> result = getWithMetadataFromSpecificMethods(methods, lookup);
|
||||
public static Method getFromSpecificMethods(final Method[] methods, final MethodMetadataLookup<?> lookup) {
|
||||
final Map.Entry<Method, ?> result = getWithMetadataFromSpecificMethods(methods, lookup);
|
||||
return Objects.isNull(result) ? null : result.getKey();
|
||||
}
|
||||
|
||||
@ -219,8 +221,9 @@ public class MethodScanner {
|
||||
* @param type 类型
|
||||
* @param lookup 查找器
|
||||
* @return 方法与对应的元数据集合
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map<Method, T> findWithMetadataFromMethods(Class<?> type, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map<Method, T> findWithMetadataFromMethods(final Class<?> type, final MethodMetadataLookup<T> lookup) {
|
||||
return findWithMetadataFromSpecificMethods(getMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -231,7 +234,7 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法集合
|
||||
*/
|
||||
public static Set<Method> findFromMethods(Class<?> type, MethodMetadataLookup<?> lookup) {
|
||||
public static Set<Method> findFromMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
|
||||
return findFromSpecificMethods(getMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -241,8 +244,9 @@ public class MethodScanner {
|
||||
* @param type 类型
|
||||
* @param lookup 查找器
|
||||
* @return 方法及元数据,若无任何匹配的结果则返回{@code null}
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromMethods(Class<?> type, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromMethods(final Class<?> type, final MethodMetadataLookup<T> lookup) {
|
||||
return getWithMetadataFromSpecificMethods(getMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -253,7 +257,7 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法,若无任何匹配的结果则返回{@code null}
|
||||
*/
|
||||
public static Method getFromMethods(Class<?> type, MethodMetadataLookup<?> lookup) {
|
||||
public static Method getFromMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
|
||||
return getFromSpecificMethods(getMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -267,8 +271,9 @@ public class MethodScanner {
|
||||
* @param type 类型
|
||||
* @param lookup 查找器
|
||||
* @return 方法与对应的元数据集合
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map<Method, T> findWithMetadataFromDeclaredMethods(Class<?> type, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map<Method, T> findWithMetadataFromDeclaredMethods(final Class<?> type, final MethodMetadataLookup<T> lookup) {
|
||||
return findWithMetadataFromSpecificMethods(getDeclaredMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -279,7 +284,7 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法集合
|
||||
*/
|
||||
public static Set<Method> findFromDeclaredMethods(Class<?> type, MethodMetadataLookup<?> lookup) {
|
||||
public static Set<Method> findFromDeclaredMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
|
||||
return findFromSpecificMethods(getDeclaredMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -289,8 +294,9 @@ public class MethodScanner {
|
||||
* @param type 类型
|
||||
* @param lookup 查找器
|
||||
* @return 方法及元数据,若无任何匹配的结果则返回{@code null}
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromDeclaredMethods(Class<?> type, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromDeclaredMethods(final Class<?> type, final MethodMetadataLookup<T> lookup) {
|
||||
return getWithMetadataFromSpecificMethods(getDeclaredMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -301,7 +307,7 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法,若无任何匹配的结果则返回{@code null}
|
||||
*/
|
||||
public static Method getFromDeclaredMethods(Class<?> type, MethodMetadataLookup<?> lookup) {
|
||||
public static Method getFromDeclaredMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
|
||||
return getFromSpecificMethods(getDeclaredMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -316,8 +322,9 @@ public class MethodScanner {
|
||||
* @param type 类型
|
||||
* @param lookup 查找器
|
||||
* @return 方法与对应的元数据集合
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map<Method, T> findWithMetadataFromAllMethods(Class<?> type, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map<Method, T> findWithMetadataFromAllMethods(final Class<?> type, final MethodMetadataLookup<T> lookup) {
|
||||
return findWithMetadataFromSpecificMethods(getAllMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -328,7 +335,7 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法集合
|
||||
*/
|
||||
public static Set<Method> findFromAllMethods(Class<?> type, MethodMetadataLookup<?> lookup) {
|
||||
public static Set<Method> findFromAllMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
|
||||
return findFromSpecificMethods(getAllMethods(type), lookup);
|
||||
}
|
||||
|
||||
@ -338,14 +345,15 @@ public class MethodScanner {
|
||||
* @param type 类型
|
||||
* @param lookup 查找器
|
||||
* @return 方法及元数据,若无任何匹配的结果则返回{@code null}
|
||||
* @param <T> 值类型
|
||||
*/
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromAllMethods(Class<?> type, MethodMetadataLookup<T> lookup) {
|
||||
public static <T> Map.Entry<Method, T> getWithMetadataFromAllMethods(final Class<?> type, final MethodMetadataLookup<T> lookup) {
|
||||
if (Objects.isNull(type)) {
|
||||
return null;
|
||||
}
|
||||
Mutable<Map.Entry<Method, T>> result = new MutableObj<>();
|
||||
final Mutable<Map.Entry<Method, T>> result = new MutableObj<>();
|
||||
ClassUtil.traverseTypeHierarchyWhile(type, t -> {
|
||||
Map.Entry<Method, T> target = getWithMetadataFromDeclaredMethods(t, lookup);
|
||||
final Map.Entry<Method, T> target = getWithMetadataFromDeclaredMethods(t, lookup);
|
||||
if (Objects.nonNull(target)) {
|
||||
result.set(target);
|
||||
return false;
|
||||
@ -362,8 +370,8 @@ public class MethodScanner {
|
||||
* @param lookup 查找器
|
||||
* @return 方法,若无任何匹配的结果则返回{@code null}
|
||||
*/
|
||||
public static Method getFromAllMethods(Class<?> type, MethodMetadataLookup<?> lookup) {
|
||||
Map.Entry<Method, ?> target = getWithMetadataFromAllMethods(type, lookup);
|
||||
public static Method getFromAllMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
|
||||
final Map.Entry<Method, ?> target = getWithMetadataFromAllMethods(type, lookup);
|
||||
return Objects.isNull(target) ? null : target.getKey();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user