mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
feat:属性拷贝选项添加属性过滤器
This commit is contained in:
parent
3f4e0540db
commit
d8de98cbfc
@ -188,6 +188,9 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||
throw new BeanException(e, "Get value of [{}] error!", prop.getFieldName());
|
||||
}
|
||||
}
|
||||
if(!copyOptions.propertiesFilter.test(prop.getField(), value)) {
|
||||
return;
|
||||
}
|
||||
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
|
||||
// 当允许跳过空时,跳过
|
||||
//值不能为bean本身,防止循环引用,此类也跳过
|
||||
@ -245,6 +248,9 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
||||
|
||||
// 获取属性值
|
||||
Object value = valueProvider.value(providerKey, fieldType);
|
||||
if(!copyOptions.propertiesFilter.test(prop.getField(), value)) {
|
||||
return;
|
||||
}
|
||||
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
|
||||
// 当允许跳过空时,跳过
|
||||
// 值不能为bean本身,防止循环引用
|
||||
|
@ -5,7 +5,9 @@ import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
/**
|
||||
* 属性拷贝选项<br>
|
||||
@ -27,6 +29,10 @@ public class CopyOptions implements Serializable {
|
||||
* 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
|
||||
*/
|
||||
protected boolean ignoreNullValue;
|
||||
/**
|
||||
* 属性过滤器,断言通过的属性才会被复制
|
||||
*/
|
||||
protected BiPredicate<Field, Object> propertiesFilter;
|
||||
/**
|
||||
* 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
||||
*/
|
||||
@ -81,6 +87,7 @@ public class CopyOptions implements Serializable {
|
||||
* 构造拷贝选项
|
||||
*/
|
||||
public CopyOptions() {
|
||||
this.propertiesFilter = (f, v) -> true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,6 +98,7 @@ public class CopyOptions implements Serializable {
|
||||
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
||||
*/
|
||||
public CopyOptions(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties) {
|
||||
this();
|
||||
this.editable = editable;
|
||||
this.ignoreNullValue = ignoreNullValue;
|
||||
this.ignoreProperties = ignoreProperties;
|
||||
@ -128,6 +136,17 @@ public class CopyOptions implements Serializable {
|
||||
return setIgnoreNullValue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 属性过滤器,断言通过的属性才会被复制
|
||||
*
|
||||
* @param propertiesFilter 属性过滤器
|
||||
* @return CopyOptions
|
||||
*/
|
||||
public CopyOptions setPropertiesFilter(BiPredicate<Field, Object> propertiesFilter) {
|
||||
this.propertiesFilter = propertiesFilter;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
|
||||
*
|
||||
|
@ -6,6 +6,7 @@ import cn.hutool.core.bean.copier.ValueProvider;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@ -461,6 +462,18 @@ public class BeanUtilTest {
|
||||
Assert.assertNull(newFood.getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void copyBeanPropertiesFilterTest() {
|
||||
Food info = new Food();
|
||||
info.setBookID("0");
|
||||
info.setCode("");
|
||||
Food newFood = new Food();
|
||||
CopyOptions copyOptions = CopyOptions.create().setPropertiesFilter((f, v) -> !(v instanceof CharSequence) || StrUtil.isNotBlank(v.toString()));
|
||||
BeanUtil.copyProperties(info, newFood, copyOptions);
|
||||
Assert.assertEquals(info.getBookID(), newFood.getBookID());
|
||||
Assert.assertNull(newFood.getCode());
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Food {
|
||||
@Alias("bookId")
|
||||
|
Loading…
x
Reference in New Issue
Block a user