mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
解决:copyProperties(x,x, CopyOptions copyOptions)
当:copyOptions的 setFieldNameEditor 不为空的时候,有bug,这里进行修复;
This commit is contained in:
@@ -237,7 +237,12 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||
return;
|
||||
}
|
||||
|
||||
final String providerKey = copyOptions.getMappedFieldName(fieldName, true);
|
||||
// 对key做映射,映射后为null的忽略之
|
||||
// 这里 copyOptions.editFieldName() 不能少,否则导致 CopyOptions setFieldNameEditor 失效
|
||||
String providerKey = copyOptions.editFieldName(copyOptions.getMappedFieldName(fieldName, false));
|
||||
if(null == providerKey){
|
||||
return;
|
||||
}
|
||||
if (false == valueProvider.containsKey(providerKey)) {
|
||||
// 无对应值可提供
|
||||
return;
|
||||
|
@@ -641,4 +641,49 @@ public class BeanUtilTest {
|
||||
int age;
|
||||
Long no;
|
||||
}
|
||||
|
||||
/**
|
||||
* @author dazer
|
||||
* copyProperties(Object source, Object target, CopyOptions copyOptions)
|
||||
* 当:copyOptions的 setFieldNameEditor 不为空的时候,有bug,这里进行修复;
|
||||
*/
|
||||
@Test
|
||||
public void beanToBeanCopyOptionsTest() {
|
||||
ChildVo1 childVo1 = new ChildVo1();
|
||||
childVo1.setChild_address("中国北京天安门");
|
||||
childVo1.setChild_name("张北京");
|
||||
childVo1.setChild_father_name("张无忌");
|
||||
childVo1.setChild_mother_name("赵敏敏");
|
||||
|
||||
CopyOptions copyOptions = CopyOptions.create().
|
||||
//setIgnoreNullValue(true).
|
||||
//setIgnoreCase(false).
|
||||
setFieldNameEditor(StrUtil::toUnderlineCase);
|
||||
|
||||
ChildVo2 childVo2 = new ChildVo2();
|
||||
BeanUtil.copyProperties(childVo1, childVo2, copyOptions);
|
||||
|
||||
Assert.assertEquals(childVo1.getChild_address(), childVo2.getChildAddress());
|
||||
Assert.assertEquals(childVo1.getChild_name(), childVo2.getChildName());
|
||||
Assert.assertEquals(childVo1.getChild_father_name(), childVo2.getChildFatherName());
|
||||
Assert.assertEquals(childVo1.getChild_mother_name(), childVo2.getChildMotherName());
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class ChildVo1 {
|
||||
String child_name;
|
||||
String child_address;
|
||||
String child_mother_name;
|
||||
String child_father_name;
|
||||
}
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public static class ChildVo2 {
|
||||
String childName;
|
||||
String childAddress;
|
||||
String childMotherName;
|
||||
String childFatherName;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user