From 54420d842930f99aa61bdcb252ccb75aae366a93 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 10 Jun 2022 22:06:39 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 3 ++- .../core/bean/copier/BeanToMapCopier.java | 2 +- .../hutool/core/bean/copier/CopyOptions.java | 6 +++-- .../core/bean/copier/MapToBeanCopier.java | 2 +- .../copier/ValueProviderToBeanCopier.java | 5 ++++ .../cn/hutool/core/bean/BeanUtilTest.java | 25 +++++++++++++++++++ 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41575df27..cc743043a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.3.M1 (2022-06-09) +# 5.8.3 (2022-06-10) ### 🐣新特性 * 【extra 】 mail增加writeTimeout参数支持(issue#2355@Github) @@ -16,6 +16,7 @@ * 【json 】 修复Bean中存在bytes,无法转换问题(issue#2365@Github) * 【core 】 ArrayUtil.setOrAppend()传入空数组时,抛出异常(issue#I5APJE@Gitee) * 【extra 】 JschSessionPool修复空指针检查问题(issue#I5BK4D@Gitee) +* 【core 】 修复使用ValueProvider中setFieldMapping无效问题(issue#I5B4R7@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java index 36a6d1f4b..441d40a28 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java @@ -25,7 +25,7 @@ public class BeanToMapCopier extends AbsCopier { * 构造 * * @param source 来源Map - * @param target 目标Bean对象 + * @param target 目标Map对象 * @param targetType 目标泛型类型 * @param copyOptions 拷贝选项 */ diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java index 2a48a38fb..85b6d216b 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/CopyOptions.java @@ -229,7 +229,8 @@ public class CopyOptions implements Serializable { } /** - * 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用 + * 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
+ * 需要注意的是,当使用ValueProvider作为数据提供者时,这个映射是相反的,即fieldMapping中key为目标Bean的名称,而value是提供者中的key * * @param fieldMapping 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用 * @return CopyOptions @@ -241,7 +242,8 @@ public class CopyOptions implements Serializable { /** * 设置字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
* 此转换器只针对源端的字段做转换,请确认转换后与目标端字段一致
- * 当转换后的字段名为null时忽略这个字段 + * 当转换后的字段名为null时忽略这个字段
+ * 需要注意的是,当使用ValueProvider作为数据提供者时,这个映射是相反的,即fieldMapping中key为目标Bean的名称,而value是提供者中的key * * @param fieldNameEditor 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等 * @return CopyOptions diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToBeanCopier.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToBeanCopier.java index 349bf7d6c..df9bdbf4b 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToBeanCopier.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToBeanCopier.java @@ -68,7 +68,7 @@ public class MapToBeanCopier extends AbsCopier, T> { } // 检查目标字段可写性 - PropDesc tDesc = findPropDesc(targetPropDescMap, sKeyStr); + final PropDesc tDesc = findPropDesc(targetPropDescMap, sKeyStr); if (null == tDesc || false == tDesc.isWritable(this.copyOptions.transientSupport)) { // 字段不可写,跳过之 return; diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/ValueProviderToBeanCopier.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/ValueProviderToBeanCopier.java index f2bc0f756..35230db3c 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/ValueProviderToBeanCopier.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/ValueProviderToBeanCopier.java @@ -49,6 +49,11 @@ public class ValueProviderToBeanCopier extends AbsCopier filedMap= new HashMap<>(); + filedMap.put("name", "sourceId"); + copyOptions.setFieldMapping(filedMap); + TestPojo pojo = BeanUtil.toBean(TestPojo.class, new ValueProvider() { + final HashMap map = new HashMap<>(); + { + map.put("sourceId", "123"); + } + @Override + public Object value(String key, Type valueType) { + return map.get(key); + } + + @Override + public boolean containsKey(String key) { + return map.containsKey(key); + } + }, copyOptions); + Assert.assertEquals("123", pojo.getName()); + } }