mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add method
This commit is contained in:
parent
79a293320a
commit
6ad682fb0a
@ -16,6 +16,7 @@
|
|||||||
* 【core 】 新增Hash接口,HashXXX继承此接口
|
* 【core 】 新增Hash接口,HashXXX继承此接口
|
||||||
* 【core 】 ZipUtil增加append方法(pr#441@Gitee)
|
* 【core 】 ZipUtil增加append方法(pr#441@Gitee)
|
||||||
* 【core 】 CollUtil增加重载(issue#I4E9FS@Gitee)
|
* 【core 】 CollUtil增加重载(issue#I4E9FS@Gitee)
|
||||||
|
* 【core 】 CopyOptions新增setFieldValueEditor(issue#I4E08T@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github)
|
* 【core 】 修复CollUtil.isEqualList两个null返回错误问题(issue#1885@Github)
|
||||||
|
@ -664,6 +664,32 @@ public class BeanUtil {
|
|||||||
).copy();
|
).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
|
// --------------------------------------------------------------------------------------------- copyProperties
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -191,6 +191,10 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
|||||||
if(null != copyOptions.propertiesFilter && false == copyOptions.propertiesFilter.test(prop.getField(), value)) {
|
if(null != copyOptions.propertiesFilter && false == copyOptions.propertiesFilter.test(prop.getField(), value)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// since 5.7.15
|
||||||
|
value = copyOptions.editFieldValue(key, value);
|
||||||
|
|
||||||
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
|
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
|
||||||
// 当允许跳过空时,跳过
|
// 当允许跳过空时,跳过
|
||||||
//值不能为bean本身,防止循环引用,此类也跳过
|
//值不能为bean本身,防止循环引用,此类也跳过
|
||||||
@ -257,6 +261,9 @@ public class BeanCopier<T> implements Copier<T>, Serializable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// since 5.7.15
|
||||||
|
value = copyOptions.editFieldValue(providerKey, value);
|
||||||
|
|
||||||
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
|
if ((null == value && copyOptions.ignoreNullValue) || bean == value) {
|
||||||
// 当允许跳过空时,跳过
|
// 当允许跳过空时,跳过
|
||||||
// 值不能为bean本身,防止循环引用
|
// 值不能为bean本身,防止循环引用
|
||||||
|
@ -7,6 +7,7 @@ import cn.hutool.core.util.ObjectUtil;
|
|||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.BiPredicate;
|
import java.util.function.BiPredicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,6 +58,10 @@ public class CopyOptions implements Serializable {
|
|||||||
* 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
|
* 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
|
||||||
*/
|
*/
|
||||||
protected Editor<String> fieldNameEditor;
|
protected Editor<String> fieldNameEditor;
|
||||||
|
/**
|
||||||
|
* 字段属性值编辑器,用于自定义属性值转换规则,例如null转""等
|
||||||
|
*/
|
||||||
|
protected BiFunction<String, Object, Object> fieldValueEditor;
|
||||||
/**
|
/**
|
||||||
* 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
* 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
||||||
*/
|
*/
|
||||||
@ -224,6 +229,30 @@ public class CopyOptions implements Serializable {
|
|||||||
return this;
|
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注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
* 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
|
||||||
*
|
*
|
||||||
@ -264,6 +293,7 @@ public class CopyOptions implements Serializable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换字段名为编辑后的字段名
|
* 转换字段名为编辑后的字段名
|
||||||
|
*
|
||||||
* @param fieldName 字段名
|
* @param fieldName 字段名
|
||||||
* @return 编辑后的字段名
|
* @return 编辑后的字段名
|
||||||
* @since 5.4.2
|
* @since 5.4.2
|
||||||
|
@ -197,6 +197,19 @@ public class BeanUtilTest {
|
|||||||
Assert.assertEquals("sub名字", map.get("sub_name"));
|
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
|
@Test
|
||||||
public void beanToMapWithAliasTest() {
|
public void beanToMapWithAliasTest() {
|
||||||
SubPersonWithAlias person = new SubPersonWithAlias();
|
SubPersonWithAlias person = new SubPersonWithAlias();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user