From 6c8fc623f04a281d96fc95c0d4480d028dc01c15 Mon Sep 17 00:00:00 2001 From: tanpenggood Date: Thu, 12 Jun 2025 01:50:00 +0800 Subject: [PATCH] =?UTF-8?q?test(ReflectUtilTest):=20ReflectUtil#getFieldMa?= =?UTF-8?q?p=20=E5=A6=82=E6=9E=9C=E5=AD=90=E7=B1=BB=E4=B8=8E=E7=88=B6?= =?UTF-8?q?=E7=B1=BB=E4=B8=AD=E5=AD=98=E5=9C=A8=E5=90=8C=E5=90=8D=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E5=88=99=E5=90=8E=E8=80=85=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E5=89=8D=E8=80=85=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/util/ReflectUtil.java | 2 +- .../cn/hutool/core/util/ReflectUtilTest.java | 34 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java index 64081f220..8ba0feee0 100755 --- a/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ReflectUtil.java @@ -153,7 +153,7 @@ public class ReflectUtil { /** * 获取指定类中字段名和字段对应的有序Map,包括其父类中的字段
- * 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。 + * 如果子类与父类中存在同名字段,则后者覆盖前者。 * * @param beanClass 类 * @return 字段名和字段对应的Map,有序 diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java index 51c06ae57..8af979421 100755 --- a/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ReflectUtilTest.java @@ -1,5 +1,6 @@ package cn.hutool.core.util; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.TimeInterval; import cn.hutool.core.date.Week; @@ -8,6 +9,7 @@ import cn.hutool.core.lang.test.bean.ExamInfoDict; import cn.hutool.core.util.ClassUtilTest.TestSubClass; import lombok.Data; import static org.junit.jupiter.api.Assertions.*; +import lombok.experimental.FieldNameConstants; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -18,6 +20,8 @@ import java.util.Collection; import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; /** * 反射工具类单元测试 @@ -79,11 +83,39 @@ public class ReflectUtilTest { assertNotNull(privateField); } + @Test + public void getFieldMapTest() { + // 获取指定类中字段名和字段对应的有序Map,包括其父类中的字段 + // 如果子类与父类中存在同名字段,则后者覆盖前者。 + Map fieldMap = ReflectUtil.getFieldMap(TestSubUser.class); + assertEquals(3, fieldMap.size()); + } + @Test public void getFieldsTest() { // 能够获取到父类字段 - final Field[] fields = ReflectUtil.getFields(TestSubClass.class); + Field[] fields = ReflectUtil.getFields(TestSubClass.class); assertEquals(4, fields.length); + + // 如果子类与父类中存在同名字段,则这两个字段同时存在,子类字段在前,父类字段在后。 + fields = ReflectUtil.getFields(TestSubUser.class); + assertEquals(4, fields.length); + List idFieldList = Arrays.asList(fields).stream().filter(f -> Objects.equals(f.getName(), TestSubUser.Fields.id)).collect(Collectors.toList()); + Field firstIdField = CollUtil.getFirst(idFieldList); + assertEquals(true, Objects.equals(firstIdField.getDeclaringClass().getName(), TestSubUser.class.getName())); + } + + @Data + static class TestBaseEntity { + private Long id; + private String remark; + } + + @Data + @FieldNameConstants + static class TestSubUser extends TestBaseEntity { + private Long id; + private String name; } @Test