fix: 修复ClassUtil.doGetMethods最后一个参数为true时,不会提前结束遍历接口

This commit is contained in:
kamosama 2024-06-05 11:22:01 +08:00
parent 86f4358ebf
commit 9cca7e69cc

View File

@ -288,43 +288,42 @@ public class ClassUtil {
private static void doGetMethods(Class<?> clazz, List<Method> methods, Predicate<Method> predicate, boolean firstOnly) { private static void doGetMethods(Class<?> clazz, List<Method> methods, Predicate<Method> predicate, boolean firstOnly) {
if (clazz == null || clazz == Object.class) { applyAllClass(clazz, currentClass -> {
return; Method[] declaredMethods = clazz.getDeclaredMethods();
} if (clazz.isInterface()) {
for (Method method : declaredMethods) {
Method[] declaredMethods = clazz.getDeclaredMethods(); // 接口类只需要获取 default 方法
if (clazz.isInterface()) { if (method.isDefault() && (predicate == null || predicate.test(method))) {
for (Method method : declaredMethods) { methods.add(method);
// 接口类只需要获取 default 方法 if (firstOnly) {
if (method.isDefault() && (predicate == null || predicate.test(method))) { break;
methods.add(method); }
if (firstOnly) { }
break; }
} else {
for (Method method : declaredMethods) {
if (predicate == null || predicate.test(method)) {
methods.add(method);
if (firstOnly) {
break;
}
} }
} }
} }
} else { // 只获取第一个并且集合不为空就结束遍历
for (Method method : declaredMethods) { if (firstOnly && !methods.isEmpty()) {
if (predicate == null || predicate.test(method)) { return false;
methods.add(method); }
if (firstOnly) { Class<?>[] interfaces = clazz.getInterfaces();
break; for (Class<?> anInterface : interfaces) {
} doGetMethods(anInterface, methods, predicate, firstOnly);
// 只获取第一个并且集合不为空就结束遍历
if (firstOnly && !methods.isEmpty()){
return false;
} }
} }
} return true;
});
if (firstOnly && !methods.isEmpty()) {
return;
}
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> anInterface : interfaces) {
doGetMethods(anInterface, methods, predicate, firstOnly);
}
doGetMethods(clazz.getSuperclass(), methods, predicate, firstOnly);
} }
private static <T> Class<T> getJdkProxySuperClass(Class<T> clazz) { private static <T> Class<T> getJdkProxySuperClass(Class<T> clazz) {