mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix
This commit is contained in:
parent
abea2f5739
commit
90f9298370
@ -26,7 +26,7 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
|
|||||||
* 构造
|
* 构造
|
||||||
*
|
*
|
||||||
* @param source 来源Map
|
* @param source 来源Map
|
||||||
* @param target 目标Bean对象
|
* @param target 目标Map对象
|
||||||
* @param targetType 目标泛型类型
|
* @param targetType 目标泛型类型
|
||||||
* @param copyOptions 拷贝选项
|
* @param copyOptions 拷贝选项
|
||||||
*/
|
*/
|
||||||
|
@ -226,7 +226,8 @@ public class CopyOptions implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
* 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用<br>
|
||||||
|
* 需要注意的是,当使用ValueProvider作为数据提供者时,这个映射是相反的,即fieldMapping中key为目标Bean的名称,而value是提供者中的key
|
||||||
*
|
*
|
||||||
* @param fieldMapping 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
* @param fieldMapping 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
|
||||||
* @return CopyOptions
|
* @return CopyOptions
|
||||||
@ -242,7 +243,8 @@ public class CopyOptions implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 设置字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等<br>
|
* 设置字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等<br>
|
||||||
* 此转换器只针对源端的字段做转换,请确认转换后与目标端字段一致<br>
|
* 此转换器只针对源端的字段做转换,请确认转换后与目标端字段一致<br>
|
||||||
* 当转换后的字段名为null时忽略这个字段
|
* 当转换后的字段名为null时忽略这个字段<br>
|
||||||
|
* 需要注意的是,当使用ValueProvider作为数据提供者时,这个映射是相反的,即参数中key为目标Bean的名称,而返回值是提供者中的key,并且对值的修改无效<br>
|
||||||
*
|
*
|
||||||
* @param editor 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
|
* @param editor 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
|
||||||
* @return CopyOptions
|
* @return CopyOptions
|
||||||
|
@ -68,7 +68,7 @@ public class MapToBeanCopier<T> extends AbsCopier<Map<?, ?>, T> {
|
|||||||
if(null == entry){
|
if(null == entry){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String sFieldName = entry.getKey();
|
final String sFieldName = entry.getKey();
|
||||||
// 对key做转换,转换后为null的跳过
|
// 对key做转换,转换后为null的跳过
|
||||||
if (null == sFieldName) {
|
if (null == sFieldName) {
|
||||||
return;
|
return;
|
||||||
|
@ -51,11 +51,6 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 无字段内容跳过
|
|
||||||
if(false == source.containsKey(tFieldName)){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 检查目标字段可写性
|
// 检查目标字段可写性
|
||||||
if (null == tDesc || false == tDesc.isWritable(this.copyOptions.transientSupport)) {
|
if (null == tDesc || false == tDesc.isWritable(this.copyOptions.transientSupport)) {
|
||||||
// 字段不可写,跳过之
|
// 字段不可写,跳过之
|
||||||
@ -64,9 +59,8 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
|
|||||||
|
|
||||||
// 获取目标字段真实类型
|
// 获取目标字段真实类型
|
||||||
final Type fieldType = TypeUtil.getActualType(this.targetType ,tDesc.getFieldType());
|
final Type fieldType = TypeUtil.getActualType(this.targetType ,tDesc.getFieldType());
|
||||||
Object sValue = source.value(tFieldName, fieldType);
|
|
||||||
// 编辑键值对
|
// 编辑键值对
|
||||||
final MutableEntry<String, Object> entry = copyOptions.editField(tFieldName, sValue);
|
final MutableEntry<String, Object> entry = copyOptions.editField(tFieldName, null);
|
||||||
if(null == entry){
|
if(null == entry){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -75,7 +69,11 @@ public class ValueProviderToBeanCopier<T> extends AbsCopier<ValueProvider<String
|
|||||||
if (null == tFieldName) {
|
if (null == tFieldName) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sValue = entry.getValue();
|
// 无字段内容跳过
|
||||||
|
if(false == source.containsKey(tFieldName)){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Object sValue = source.value(tFieldName, fieldType);
|
||||||
|
|
||||||
// 检查目标对象属性是否过滤属性
|
// 检查目标对象属性是否过滤属性
|
||||||
if (false == copyOptions.testPropertyFilter(tDesc.getField(), sValue)) {
|
if (false == copyOptions.testPropertyFilter(tDesc.getField(), sValue)) {
|
||||||
|
@ -825,4 +825,29 @@ public class BeanUtilTest {
|
|||||||
public static class WkCrmCustomer{
|
public static class WkCrmCustomer{
|
||||||
private LocalDateTime statusIdUpdateTime;
|
private LocalDateTime statusIdUpdateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void valueProviderToBeanTest(){
|
||||||
|
// https://gitee.com/dromara/hutool/issues/I5B4R7
|
||||||
|
final CopyOptions copyOptions = CopyOptions.of();
|
||||||
|
final Map<String, String> filedMap= new HashMap<>();
|
||||||
|
filedMap.put("name", "sourceId");
|
||||||
|
copyOptions.setFieldMapping(filedMap);
|
||||||
|
final TestPojo pojo = BeanUtil.fillBean(new TestPojo(), new ValueProvider<String>() {
|
||||||
|
final HashMap<String, Object> map = new HashMap<>();
|
||||||
|
{
|
||||||
|
map.put("sourceId", "123");
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Object value(final String key, final Type valueType) {
|
||||||
|
return map.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsKey(final String key) {
|
||||||
|
return map.containsKey(key);
|
||||||
|
}
|
||||||
|
}, copyOptions);
|
||||||
|
Assert.assertEquals("123", pojo.getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user