From 9cca7e69cced59f7c5be270d70cff895d0282f4a Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Wed, 5 Jun 2024 11:22:01 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DClassUtil.doGetMethods?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0=E4=B8=BA?= =?UTF-8?q?true=E6=97=B6,=E4=B8=8D=E4=BC=9A=E6=8F=90=E5=89=8D=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E9=81=8D=E5=8E=86=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/util/ClassUtil.java | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java index 3fa98b0f..6d2e019e 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/ClassUtil.java @@ -288,43 +288,42 @@ public class ClassUtil { private static void doGetMethods(Class clazz, List methods, Predicate 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 Class getJdkProxySuperClass(Class clazz) {