fix(hutool-core): 修复子类使用同名属性覆盖了父类的transient属性导致属性transient检测而被忽略

This commit is contained in:
xyz327 2020-10-20 15:27:03 +08:00
parent c008d0a081
commit 995bacc253
2 changed files with 35 additions and 2 deletions

View File

@ -146,7 +146,9 @@ public class BeanDesc implements Serializable {
if (false == ModifierUtil.isStatic(field)) {
//只针对非static属性
prop = createProp(field, methods);
this.propMap.put(prop.getFieldName(), prop);
PropDesc finalProp = prop;
// 只有不存在时才放入防止父类属性覆盖子类属性
this.propMap.putIfAbsent(prop.getFieldName(), finalProp);
}
}
return this;

View File

@ -364,12 +364,26 @@ public class BeanUtilTest {
@Getter
@Setter
public static class Person {
public static class SubPersonWithOverlayTransientField extends PersonWithTransientField {
// 覆盖父类中 transient 属性
private String name;
}
@Getter
@Setter
public static class Person {
private String name;
private int age;
private String openid;
}
@Getter
@Setter
public static class PersonWithTransientField {
private transient String name;
private int age;
private String openid;
}
public static class Person2 {
public Person2(String name, int age, String openid) {
@ -383,6 +397,23 @@ public class BeanUtilTest {
public String openid;
}
/**
* <a href="https://github.com/looly/hutool/issues/1173">#1173</a>
*/
@Test
public void beanToBeanOverlayFieldTest() {
SubPersonWithOverlayTransientField source = new SubPersonWithOverlayTransientField();
source.setName("zhangsan");
source.setAge(20);
source.setOpenid("1");
SubPersonWithOverlayTransientField dest = new SubPersonWithOverlayTransientField();
BeanUtil.copyProperties(source, dest);
Assert.assertEquals(source.getName(), dest.getName());
Assert.assertEquals(source.getAge(), dest.getAge());
Assert.assertEquals(source.getOpenid(), dest.getOpenid());
}
@Test
public void beanToBeanTest() {
// 修复对象无getter方法导致报错的问题