From 3d03f7140ac68433d6787a5f6be4ef4efad74107 Mon Sep 17 00:00:00 2001 From: ruying408 <1877972603@qq.com> Date: Thu, 9 Jan 2025 23:50:08 +0800 Subject: [PATCH] =?UTF-8?q?eps=E5=B0=86=E7=88=B6=E7=B1=BB=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E4=B8=80=E5=B9=B6=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/cool/core/eps/CoolEps.java | 28 +++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/cool/core/eps/CoolEps.java b/src/main/java/com/cool/core/eps/CoolEps.java index b79430c..a041676 100644 --- a/src/main/java/com/cool/core/eps/CoolEps.java +++ b/src/main/java/com/cool/core/eps/CoolEps.java @@ -281,12 +281,38 @@ public class CoolEps { Set> classes = ClassUtil.scanPackageByAnnotation("", Table.class); classes.forEach(e -> { // 获得属性 - Field[] fields = ClassUtil.getDeclaredFields(e); + Field[] fields = getAllDeclaredFields(e); List columns = columns(fields); entityInfo.set(e.getSimpleName(), columns); }); } + /** + * 获取类及其所有父类中声明的字段 + * + * @param clazz 要检查的类 + * @return 包含类及其所有父类中声明的所有字段的数组 + */ + public static Field[] getAllDeclaredFields(Class clazz) { + // 参数校验 + if (clazz == null) { + throw new IllegalArgumentException("Class cannot be null"); + } + + List allFields = new ArrayList<>(); + Class currentClass = clazz; + + // 循环遍历类及其父类 + while (currentClass != null) { + Field[] declaredFields = currentClass.getDeclaredFields(); + allFields.addAll(Arrays.asList(declaredFields)); + currentClass = currentClass.getSuperclass(); + } + + // 将列表转换为数组返回 + return allFields.toArray(new Field[0]); + } + /** * 获得所有的列 *