From 6d5ca7051b1ddc584eaf57abff20774c6b494b6f Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 19 Mar 2021 16:46:36 +0800 Subject: [PATCH] add propertiesFilter --- CHANGELOG.md | 3 ++- .../src/main/java/cn/hutool/core/bean/copier/BeanCopier.java | 5 +++-- .../main/java/cn/hutool/core/bean/copier/CopyOptions.java | 3 +-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eb030cf1..6a196abef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,11 @@ ------------------------------------------------------------------------------------------------------------- -# 5.6.2 (2021-03-18) +# 5.6.2 (2021-03-19) ### 新特性 * 【core 】 Validator增加车架号(车辆识别码)验证、驾驶证(驾驶证档案编号)的正则校验(pr#280@Gitee) +* 【core 】 CopyOptions增加propertiesFilter(pr#281@Gitee) ### Bug修复 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanCopier.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanCopier.java index f9ef1239c..dd7a83cbd 100644 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanCopier.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanCopier.java @@ -188,7 +188,7 @@ public class BeanCopier implements Copier, Serializable { throw new BeanException(e, "Get value of [{}] error!", prop.getFieldName()); } } - if(!copyOptions.propertiesFilter.test(prop.getField(), value)) { + if(null != copyOptions.propertiesFilter && false == copyOptions.propertiesFilter.test(prop.getField(), value)) { return; } if ((null == value && copyOptions.ignoreNullValue) || bean == value) { @@ -248,9 +248,10 @@ public class BeanCopier implements Copier, Serializable { // 获取属性值 Object value = valueProvider.value(providerKey, fieldType); - if(!copyOptions.propertiesFilter.test(prop.getField(), value)) { + if(null != copyOptions.propertiesFilter && false == copyOptions.propertiesFilter.test(prop.getField(), value)) { return; } + if ((null == value && copyOptions.ignoreNullValue) || bean == value) { // 当允许跳过空时,跳过 // 值不能为bean本身,防止循环引用 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 fdc759aba..502ea4846 100644 --- 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 @@ -87,7 +87,6 @@ public class CopyOptions implements Serializable { * 构造拷贝选项 */ public CopyOptions() { - this.propertiesFilter = (f, v) -> true; } /** @@ -98,7 +97,7 @@ public class CopyOptions implements Serializable { * @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值 */ public CopyOptions(Class editable, boolean ignoreNullValue, String... ignoreProperties) { - this(); + this.propertiesFilter = (f, v) -> true; this.editable = editable; this.ignoreNullValue = ignoreNullValue; this.ignoreProperties = ignoreProperties;