This commit is contained in:
Looly 2023-05-29 00:20:04 +08:00
parent 33197358f8
commit 9c4d71d36a
4 changed files with 50 additions and 50 deletions

View File

@ -17,24 +17,15 @@ import java.util.function.Predicate;
@FunctionalInterface @FunctionalInterface
public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<Method> { public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<Method> {
/**
* 检查方法是否匹配
*
* @param method 方法
* @return 结果
*/
boolean test(Method method);
/** /**
* 返回一个组合的条件当且仅当所有条件都符合时才返回{@code true} * 返回一个组合的条件当且仅当所有条件都符合时才返回{@code true}
* 等同于{@link Predicate#and(Predicate)}.
* *
* @param other 其他条件 * @param other 其他条件
* @return 条件 * @return 条件
* @throws NullPointerException 当other为null时抛出 * @throws NullPointerException 当other为null时抛出
*/ */
@Override @Override
default MethodMatcher and(Predicate<? super Method> other) { default MethodMatcher and(final Predicate<? super Method> other) {
Objects.requireNonNull(other); Objects.requireNonNull(other);
return t -> test(t) && other.test(t); return t -> test(t) && other.test(t);
} }
@ -57,7 +48,7 @@ public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<
* @throws NullPointerException 当other为null时抛出 * @throws NullPointerException 当other为null时抛出
*/ */
@Override @Override
default MethodMatcher or(Predicate<? super Method> other) { default MethodMatcher or(final Predicate<? super Method> other) {
Objects.requireNonNull(other); Objects.requireNonNull(other);
return t -> test(t) || other.test(t); return t -> test(t) || other.test(t);
} }
@ -69,7 +60,7 @@ public interface MethodMatcher extends MethodMetadataLookup<Boolean>, Predicate<
* @return 结果 * @return 结果
*/ */
@Override @Override
default Boolean inspect(Method method) { default Boolean inspect(final Method method) {
return test(method) ? Boolean.TRUE : null; return test(method) ? Boolean.TRUE : null;
} }
} }

View File

@ -318,7 +318,7 @@ public class MethodMatcherUtil {
* @return 方法匹配器 * @return 方法匹配器
*/ */
public static MethodMatcher forNameIgnoreCaseAndStrictParameterTypes( public static MethodMatcher forNameIgnoreCaseAndStrictParameterTypes(
final String methodName, Class<?>... parameterTypes) { final String methodName, final Class<?>... parameterTypes) {
Objects.requireNonNull(methodName); Objects.requireNonNull(methodName);
Objects.requireNonNull(parameterTypes); Objects.requireNonNull(parameterTypes);
return allMatch(forNameIgnoreCase(methodName), forStrictParameterTypes(parameterTypes)); return allMatch(forNameIgnoreCase(methodName), forStrictParameterTypes(parameterTypes));
@ -352,8 +352,8 @@ public class MethodMatcherUtil {
* <li>参数类型是否匹配允许参数类型为方法参数类型的子类若参数类型为{@code null}则表示匹配无参数的方法</li> * <li>参数类型是否匹配允许参数类型为方法参数类型的子类若参数类型为{@code null}则表示匹配无参数的方法</li>
* </ul> * </ul>
* *
* @param methodName 方法名 * @param methodName 方法名
* @param returnType 返回值类型若为{@code null}则表示匹配无返回值的方法 * @param returnType 返回值类型若为{@code null}则表示匹配无返回值的方法
* @param parameterTypes 参数类型若为{@code null}则表示匹配无参数的方法 * @param parameterTypes 参数类型若为{@code null}则表示匹配无参数的方法
* @return 方法匹配器 * @return 方法匹配器
*/ */
@ -375,8 +375,8 @@ public class MethodMatcherUtil {
* <li>参数类型是否匹配要求参数类型与方法参数类型完全一致若参数类型为{@code null}则表示匹配无参数的方法</li> * <li>参数类型是否匹配要求参数类型与方法参数类型完全一致若参数类型为{@code null}则表示匹配无参数的方法</li>
* </ul> * </ul>
* *
* @param methodName 方法名 * @param methodName 方法名
* @param returnType 返回值类型若为{@code null}则表示匹配无返回值的方法 * @param returnType 返回值类型若为{@code null}则表示匹配无返回值的方法
* @param parameterTypes 参数类型若为{@code null}则表示匹配无参数的方法 * @param parameterTypes 参数类型若为{@code null}则表示匹配无参数的方法
* @return 方法匹配器 * @return 方法匹配器
*/ */
@ -474,7 +474,7 @@ public class MethodMatcherUtil {
* @param count 参数个数 * @param count 参数个数
* @return 方法匹配器 * @return 方法匹配器
*/ */
public static MethodMatcher forParameterCount(int count) { public static MethodMatcher forParameterCount(final int count) {
return method -> method.getParameterCount() == count; return method -> method.getParameterCount() == count;
} }
@ -568,7 +568,7 @@ public class MethodMatcherUtil {
@NotNull @NotNull
private static MethodMatcher mostSpecificStrictParameterTypesMatcher( private static MethodMatcher mostSpecificStrictParameterTypesMatcher(
Class<?>[] parameterTypes, BiPredicate<Class<?>, Class<?>> typeMatcher) { final Class<?>[] parameterTypes, final BiPredicate<Class<?>, Class<?>> typeMatcher) {
Objects.requireNonNull(parameterTypes); Objects.requireNonNull(parameterTypes);
// 若参数为空则表示匹配无参数方法 // 若参数为空则表示匹配无参数方法
if (parameterTypes.length == 0) { if (parameterTypes.length == 0) {

View File

@ -5,6 +5,7 @@ import java.lang.reflect.Method;
/** /**
* 方法的元数据查找器参照 spring {@code MethodIntrospector.MetadataLookup}用于从方法上获得特定的元数据 * 方法的元数据查找器参照 spring {@code MethodIntrospector.MetadataLookup}用于从方法上获得特定的元数据
* *
* @param <T> 返回类型
* @author huangchengxing * @author huangchengxing
* @see MethodMatcher * @see MethodMatcher
* @see MethodMatcherUtil * @see MethodMatcherUtil

View File

@ -63,8 +63,8 @@ import java.util.function.Predicate;
* @since 6.0.0 * @since 6.0.0
*/ */
public class MethodScanner { public class MethodScanner {
/** /*
* TODO 替换{@link MethodUtil}中的部分方法实现 TODO 替换{@link MethodUtil}中的部分方法实现
*/ */
/** /**
@ -90,7 +90,7 @@ public class MethodScanner {
* @param type * @param type
* @return 当前类及父类的所有公共方法 * @return 当前类及父类的所有公共方法
*/ */
public static Method[] getMethods(Class<?> type) { public static Method[] getMethods(final Class<?> type) {
if (Objects.isNull(type)) { if (Objects.isNull(type)) {
return EMPTY_METHODS; return EMPTY_METHODS;
} }
@ -103,7 +103,7 @@ public class MethodScanner {
* @param type * @param type
* @return 当前类及父类的所有公共方法 * @return 当前类及父类的所有公共方法
*/ */
public static Method[] getDeclaredMethods(Class<?> type) { public static Method[] getDeclaredMethods(final Class<?> type) {
if (Objects.isNull(type)) { if (Objects.isNull(type)) {
return EMPTY_METHODS; return EMPTY_METHODS;
} }
@ -124,11 +124,11 @@ public class MethodScanner {
* @return 当前类及父类的所有公共方法 * @return 当前类及父类的所有公共方法
* @see ClassUtil#traverseTypeHierarchyWhile(Class, Predicate) * @see ClassUtil#traverseTypeHierarchyWhile(Class, Predicate)
*/ */
public static Method[] getAllMethods(Class<?> type) { public static Method[] getAllMethods(final Class<?> type) {
if (Objects.isNull(type)) { if (Objects.isNull(type)) {
return EMPTY_METHODS; return EMPTY_METHODS;
} }
List<Method> methods = new ArrayList<>(); final List<Method> methods = new ArrayList<>();
ClassUtil.traverseTypeHierarchyWhile(type, t -> { ClassUtil.traverseTypeHierarchyWhile(type, t -> {
methods.addAll(Arrays.asList(getDeclaredMethods(t))); methods.addAll(Arrays.asList(getDeclaredMethods(t)));
return true; return true;
@ -154,14 +154,15 @@ public class MethodScanner {
* @param methods 方法列表 * @param methods 方法列表
* @param lookup 查找器 * @param lookup 查找器
* @return 方法与对应的元数据集合 * @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)) { if (ArrayUtil.isEmpty(methods)) {
return Collections.emptyMap(); return Collections.emptyMap();
} }
Map<Method, T> results = new LinkedHashMap<>(); final Map<Method, T> results = new LinkedHashMap<>();
for (Method method : methods) { for (final Method method : methods) {
T result = lookup.inspect(method); final T result = lookup.inspect(method);
if (Objects.nonNull(result)) { if (Objects.nonNull(result)) {
results.put(method, result); results.put(method, result);
} }
@ -176,7 +177,7 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法集合 * @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(); return findWithMetadataFromSpecificMethods(methods, lookup).keySet();
} }
@ -186,10 +187,11 @@ public class MethodScanner {
* @param methods 方法列表 * @param methods 方法列表
* @param lookup 查找器 * @param lookup 查找器
* @return 方法与对应的元数据 * @return 方法与对应的元数据
* @param <T> 值类型
*/ */
public static <T> Map.Entry<Method, T> getWithMetadataFromSpecificMethods(Method[] methods, MethodMetadataLookup<T> lookup) { public static <T> Map.Entry<Method, T> getWithMetadataFromSpecificMethods(final Method[] methods, final MethodMetadataLookup<T> lookup) {
for (Method method : methods) { for (final Method method : methods) {
T result = lookup.inspect(method); final T result = lookup.inspect(method);
if (Objects.nonNull(result)) { if (Objects.nonNull(result)) {
return MapUtil.entry(method, result); return MapUtil.entry(method, result);
} }
@ -204,8 +206,8 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法 * @return 方法
*/ */
public static Method getFromSpecificMethods(Method[] methods, MethodMetadataLookup<?> lookup) { public static Method getFromSpecificMethods(final Method[] methods, final MethodMetadataLookup<?> lookup) {
Map.Entry<Method, ?> result = getWithMetadataFromSpecificMethods(methods, lookup); final Map.Entry<Method, ?> result = getWithMetadataFromSpecificMethods(methods, lookup);
return Objects.isNull(result) ? null : result.getKey(); return Objects.isNull(result) ? null : result.getKey();
} }
@ -219,8 +221,9 @@ public class MethodScanner {
* @param type 类型 * @param type 类型
* @param lookup 查找器 * @param lookup 查找器
* @return 方法与对应的元数据集合 * @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); return findWithMetadataFromSpecificMethods(getMethods(type), lookup);
} }
@ -231,7 +234,7 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法集合 * @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); return findFromSpecificMethods(getMethods(type), lookup);
} }
@ -241,8 +244,9 @@ public class MethodScanner {
* @param type 类型 * @param type 类型
* @param lookup 查找器 * @param lookup 查找器
* @return 方法及元数据若无任何匹配的结果则返回{@code null} * @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); return getWithMetadataFromSpecificMethods(getMethods(type), lookup);
} }
@ -253,7 +257,7 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法若无任何匹配的结果则返回{@code null} * @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); return getFromSpecificMethods(getMethods(type), lookup);
} }
@ -267,8 +271,9 @@ public class MethodScanner {
* @param type 类型 * @param type 类型
* @param lookup 查找器 * @param lookup 查找器
* @return 方法与对应的元数据集合 * @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); return findWithMetadataFromSpecificMethods(getDeclaredMethods(type), lookup);
} }
@ -279,7 +284,7 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法集合 * @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); return findFromSpecificMethods(getDeclaredMethods(type), lookup);
} }
@ -289,8 +294,9 @@ public class MethodScanner {
* @param type 类型 * @param type 类型
* @param lookup 查找器 * @param lookup 查找器
* @return 方法及元数据若无任何匹配的结果则返回{@code null} * @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); return getWithMetadataFromSpecificMethods(getDeclaredMethods(type), lookup);
} }
@ -301,7 +307,7 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法若无任何匹配的结果则返回{@code null} * @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); return getFromSpecificMethods(getDeclaredMethods(type), lookup);
} }
@ -316,8 +322,9 @@ public class MethodScanner {
* @param type 类型 * @param type 类型
* @param lookup 查找器 * @param lookup 查找器
* @return 方法与对应的元数据集合 * @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); return findWithMetadataFromSpecificMethods(getAllMethods(type), lookup);
} }
@ -328,7 +335,7 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法集合 * @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); return findFromSpecificMethods(getAllMethods(type), lookup);
} }
@ -338,14 +345,15 @@ public class MethodScanner {
* @param type 类型 * @param type 类型
* @param lookup 查找器 * @param lookup 查找器
* @return 方法及元数据若无任何匹配的结果则返回{@code null} * @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)) { if (Objects.isNull(type)) {
return null; return null;
} }
Mutable<Map.Entry<Method, T>> result = new MutableObj<>(); final Mutable<Map.Entry<Method, T>> result = new MutableObj<>();
ClassUtil.traverseTypeHierarchyWhile(type, t -> { 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)) { if (Objects.nonNull(target)) {
result.set(target); result.set(target);
return false; return false;
@ -362,8 +370,8 @@ public class MethodScanner {
* @param lookup 查找器 * @param lookup 查找器
* @return 方法若无任何匹配的结果则返回{@code null} * @return 方法若无任何匹配的结果则返回{@code null}
*/ */
public static Method getFromAllMethods(Class<?> type, MethodMetadataLookup<?> lookup) { public static Method getFromAllMethods(final Class<?> type, final MethodMetadataLookup<?> lookup) {
Map.Entry<Method, ?> target = getWithMetadataFromAllMethods(type, lookup); final Map.Entry<Method, ?> target = getWithMetadataFromAllMethods(type, lookup);
return Objects.isNull(target) ? null : target.getKey(); return Objects.isNull(target) ? null : target.getKey();
} }