add method

This commit is contained in:
Looly 2021-10-21 14:24:02 +08:00
parent 79a293320a
commit 6ad682fb0a
5 changed files with 83 additions and 6 deletions

View File

@ -16,6 +16,7 @@
* 【core 】 新增Hash接口HashXXX继承此接口
* 【core 】 ZipUtil增加append方法pr#441@Gitee
* 【core 】 CollUtil增加重载issue#I4E9FS@Gitee
* 【core 】 CopyOptions新增setFieldValueEditorissue#I4E08T@Gitee
### 🐞Bug修复
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题issue#1885@Github

View File

@ -664,6 +664,32 @@ public class BeanUtil {
).copy();
}
/**
* 对象转Map<br>
* 通过自定义{@link CopyOptions} 完成抓换选项以便实现
*
* <pre>
* 1. 字段筛选可以去除不需要的字段
* 2. 字段变换例如实现驼峰转下划线
* 3. 自定义字段前缀或后缀等等
* 4. 字段值处理
* ...
* </pre>
*
* @param bean bean对象
* @param targetMap 目标的Map
* @param copyOptions 拷贝选项
* @return Map
* @since 5.7.15
*/
public static Map<String, Object> beanToMap(Object bean, Map<String, Object> targetMap, CopyOptions copyOptions) {
if (null == bean) {
return null;
}
return BeanCopier.create(bean, targetMap, copyOptions).copy();
}
// --------------------------------------------------------------------------------------------- copyProperties
/**

View File

@ -191,6 +191,10 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
if(null != copyOptions.propertiesFilter && false == copyOptions.propertiesFilter.test(prop.getField(), value)) {
return;
}
// since 5.7.15
value = copyOptions.editFieldValue(key, value);
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
// 当允许跳过空时跳过
//值不能为bean本身防止循环引用此类也跳过
@ -257,6 +261,9 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
return;
}
// since 5.7.15
value = copyOptions.editFieldValue(providerKey, value);
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
// 当允许跳过空时跳过
// 值不能为bean本身防止循环引用

View File

@ -7,6 +7,7 @@ import cn.hutool.core.util.ObjectUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
/**
@ -57,6 +58,10 @@ public class CopyOptions implements Serializable {
* 字段属性编辑器用于自定义属性转换规则例如驼峰转下划线等
*/
protected Editor<String> fieldNameEditor;
/**
* 字段属性值编辑器用于自定义属性值转换规则例如null转""
*/
protected BiFunction<String, Object, Object> fieldValueEditor;
/**
* 是否支持transient关键字修饰和@Transient注解如果支持被修饰的字段或方法对应的字段将被忽略
*/
@ -224,6 +229,30 @@ public class CopyOptions implements Serializable {
return this;
}
/**
* 设置字段属性值编辑器用于自定义属性值转换规则例如null转""<br>
*
* @param fieldValueEditor 字段属性值编辑器用于自定义属性值转换规则例如null转""
* @return CopyOptions
* @since 5.7.15
*/
public CopyOptions setFieldValueEditor(BiFunction<String, Object, Object> fieldValueEditor) {
this.fieldValueEditor = fieldValueEditor;
return this;
}
/**
* 转换字段名为编辑后的字段名
*
* @param fieldName 字段名
* @return 编辑后的字段名
* @since 5.7.15
*/
protected Object editFieldValue(String fieldName, Object fieldValue) {
return (null != this.fieldValueEditor) ?
this.fieldValueEditor.apply(fieldName, fieldValue) : fieldValue;
}
/**
* 是否支持transient关键字修饰和@Transient注解如果支持被修饰的字段或方法对应的字段将被忽略
*
@ -251,12 +280,12 @@ public class CopyOptions implements Serializable {
* 当非反向则根据源字段名获取目标字段名反之根据目标字段名获取源字段名
*
* @param fieldName 字段名
* @param reversed 是否反向映射
* @param reversed 是否反向映射
* @return 映射后的字段名
*/
protected String getMappedFieldName(String fieldName, boolean reversed){
protected String getMappedFieldName(String fieldName, boolean reversed) {
Map<String, String> mapping = reversed ? getReversedMapping() : this.fieldMapping;
if(MapUtil.isEmpty(mapping)){
if (MapUtil.isEmpty(mapping)) {
return fieldName;
}
return ObjectUtil.defaultIfNull(mapping.get(fieldName), fieldName);
@ -264,11 +293,12 @@ public class CopyOptions implements Serializable {
/**
* 转换字段名为编辑后的字段名
*
* @param fieldName 字段名
* @return 编辑后的字段名
* @since 5.4.2
*/
protected String editFieldName(String fieldName){
protected String editFieldName(String fieldName) {
return (null != this.fieldNameEditor) ? this.fieldNameEditor.edit(fieldName) : fieldName;
}
@ -279,10 +309,10 @@ public class CopyOptions implements Serializable {
* @since 4.1.10
*/
private Map<String, String> getReversedMapping() {
if(null == this.fieldMapping){
if (null == this.fieldMapping) {
return null;
}
if(null == this.reversedFieldMapping){
if (null == this.reversedFieldMapping) {
reversedFieldMapping = MapUtil.reverse(this.fieldMapping);
}
return reversedFieldMapping;

View File

@ -197,6 +197,19 @@ public class BeanUtilTest {
Assert.assertEquals("sub名字", map.get("sub_name"));
}
@Test
public void beanToMapWithValueEditTest() {
SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
person.setName("测试A11");
person.setSubName("sub名字");
Map<String, Object> map = BeanUtil.beanToMap(person, new LinkedHashMap<>(),
CopyOptions.create().setFieldValueEditor((key, value) -> key + "_" + value));
Assert.assertEquals("subName_sub名字", map.get("subName"));
}
@Test
public void beanToMapWithAliasTest() {
SubPersonWithAlias person = new SubPersonWithAlias();