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