mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add TypeConverter
This commit is contained in:
parent
c2707b6180
commit
11d7d8c092
@ -19,6 +19,7 @@
|
|||||||
* 【core 】 增加Pid,以便获取单例pid
|
* 【core 】 增加Pid,以便获取单例pid
|
||||||
* 【core 】 Img增加全覆盖水印pressTextFull(pr#595@Gitee)
|
* 【core 】 Img增加全覆盖水印pressTextFull(pr#595@Gitee)
|
||||||
* 【core 】 ByteUtil.numberToBytes增加Byte判断(issue#2252@Github)
|
* 【core 】 ByteUtil.numberToBytes增加Byte判断(issue#2252@Github)
|
||||||
|
* 【core 】 CopyOptions添加converter,可以自定义非全局类型转换
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复UserAgentUtil识别Linux出错(issue#I50YGY@Gitee)
|
* 【core 】 修复UserAgentUtil识别Linux出错(issue#I50YGY@Gitee)
|
||||||
|
@ -2,7 +2,6 @@ package cn.hutool.core.bean.copier;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.bean.PropDesc;
|
import cn.hutool.core.bean.PropDesc;
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.TypeUtil;
|
import cn.hutool.core.util.TypeUtil;
|
||||||
|
|
||||||
@ -75,7 +74,8 @@ public class BeanToBeanCopier<S, T> extends AbsCopier<S, T> {
|
|||||||
|
|
||||||
// 获取目标字段真实类型并转换源值
|
// 获取目标字段真实类型并转换源值
|
||||||
final Type fieldType = TypeUtil.getActualType(this.targetType, tDesc.getFieldType());
|
final Type fieldType = TypeUtil.getActualType(this.targetType, tDesc.getFieldType());
|
||||||
sValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
|
//sValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
|
||||||
|
sValue = this.copyOptions.convertField(fieldType, sValue);
|
||||||
sValue = copyOptions.editFieldValue(sFieldName, sValue);
|
sValue = copyOptions.editFieldValue(sFieldName, sValue);
|
||||||
|
|
||||||
// 目标赋值
|
// 目标赋值
|
||||||
|
@ -2,7 +2,6 @@ package cn.hutool.core.bean.copier;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.bean.PropDesc;
|
import cn.hutool.core.bean.PropDesc;
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.TypeUtil;
|
import cn.hutool.core.util.TypeUtil;
|
||||||
|
|
||||||
@ -67,7 +66,8 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
|
|||||||
// 获取目标值真实类型并转换源值
|
// 获取目标值真实类型并转换源值
|
||||||
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
|
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
|
||||||
if(null != typeArguments){
|
if(null != typeArguments){
|
||||||
sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
||||||
|
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
|
||||||
sValue = copyOptions.editFieldValue(sFieldName, sValue);
|
sValue = copyOptions.editFieldValue(sFieldName, sValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.hutool.core.bean.copier;
|
package cn.hutool.core.bean.copier;
|
||||||
|
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
|
import cn.hutool.core.convert.TypeConverter;
|
||||||
import cn.hutool.core.lang.Editor;
|
import cn.hutool.core.lang.Editor;
|
||||||
import cn.hutool.core.lang.func.Func1;
|
import cn.hutool.core.lang.func.Func1;
|
||||||
import cn.hutool.core.lang.func.LambdaUtil;
|
import cn.hutool.core.lang.func.LambdaUtil;
|
||||||
@ -7,6 +9,7 @@ import cn.hutool.core.util.ArrayUtil;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
@ -64,7 +67,14 @@ public class CopyOptions implements Serializable {
|
|||||||
*/
|
*/
|
||||||
protected boolean override = true;
|
protected boolean override = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义类型转换器,默认使用全局万能转换器转换
|
||||||
|
*/
|
||||||
|
protected TypeConverter converter = (type, value) ->
|
||||||
|
Convert.convertWithCheck(type, value, null, ignoreError);
|
||||||
|
|
||||||
//region create
|
//region create
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建拷贝选项
|
* 创建拷贝选项
|
||||||
*
|
*
|
||||||
@ -225,7 +235,7 @@ public class CopyOptions implements Serializable {
|
|||||||
* @return CopyOptions
|
* @return CopyOptions
|
||||||
*/
|
*/
|
||||||
public CopyOptions setFieldMapping(Map<String, String> fieldMapping) {
|
public CopyOptions setFieldMapping(Map<String, String> fieldMapping) {
|
||||||
return setFieldNameEditor((key-> fieldMapping.getOrDefault(key, key)));
|
return setFieldNameEditor((key -> fieldMapping.getOrDefault(key, key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -291,6 +301,32 @@ public class CopyOptions implements Serializable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置自定义类型转换器,默认使用全局万能转换器转换。
|
||||||
|
*
|
||||||
|
* @param converter 转换器
|
||||||
|
* @return this
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
public CopyOptions setConverter(TypeConverter converter) {
|
||||||
|
this.converter = converter;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用自定义转换器转换字段值<br>
|
||||||
|
* 如果自定义转换器为{@code null},则返回原值。
|
||||||
|
*
|
||||||
|
* @param targetType 目标类型
|
||||||
|
* @param fieldValue 字段值
|
||||||
|
* @return 编辑后的字段值
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
protected Object convertField(Type targetType, Object fieldValue) {
|
||||||
|
return (null != this.converter) ?
|
||||||
|
this.converter.convert(targetType, fieldValue) : fieldValue;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换字段名为编辑后的字段名
|
* 转换字段名为编辑后的字段名
|
||||||
*
|
*
|
||||||
|
@ -2,7 +2,6 @@ package cn.hutool.core.bean.copier;
|
|||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.core.bean.PropDesc;
|
import cn.hutool.core.bean.PropDesc;
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.map.CaseInsensitiveMap;
|
import cn.hutool.core.map.CaseInsensitiveMap;
|
||||||
import cn.hutool.core.map.MapWrapper;
|
import cn.hutool.core.map.MapWrapper;
|
||||||
@ -83,7 +82,8 @@ public class MapToBeanCopier<T> extends AbsCopier<Map<?, ?>, T> {
|
|||||||
|
|
||||||
// 获取目标字段真实类型并转换源值
|
// 获取目标字段真实类型并转换源值
|
||||||
final Type fieldType = TypeUtil.getActualType(this.targetType, tDesc.getFieldType());
|
final Type fieldType = TypeUtil.getActualType(this.targetType, tDesc.getFieldType());
|
||||||
Object newValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
|
//Object newValue = Convert.convertWithCheck(fieldType, sValue, null, this.copyOptions.ignoreError);
|
||||||
|
Object newValue = this.copyOptions.convertField(fieldType, sValue);
|
||||||
newValue = copyOptions.editFieldValue(sKeyStr, newValue);
|
newValue = copyOptions.editFieldValue(sKeyStr, newValue);
|
||||||
|
|
||||||
// 目标赋值
|
// 目标赋值
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package cn.hutool.core.bean.copier;
|
package cn.hutool.core.bean.copier;
|
||||||
|
|
||||||
import cn.hutool.core.convert.Convert;
|
|
||||||
import cn.hutool.core.util.TypeUtil;
|
import cn.hutool.core.util.TypeUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
@ -53,7 +52,8 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
|
|||||||
// 获取目标值真实类型并转换源值
|
// 获取目标值真实类型并转换源值
|
||||||
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
|
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
|
||||||
if(null != typeArguments){
|
if(null != typeArguments){
|
||||||
sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
|
||||||
|
sValue = this.copyOptions.convertField(typeArguments[1], sValue);
|
||||||
sValue = copyOptions.editFieldValue(sKeyStr, sValue);
|
sValue = copyOptions.editFieldValue(sKeyStr, sValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package cn.hutool.core.convert;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 类型转换接口函数,根据给定的值和目标类型,由用户自定义转换规则。
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.8.0
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface TypeConverter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转换为指定类型<br>
|
||||||
|
* 如果类型无法确定,将读取默认值的类型做为目标类型
|
||||||
|
*
|
||||||
|
* @param targetType 目标Type,非泛型类使用
|
||||||
|
* @param value 原始值
|
||||||
|
* @return 转换后的值
|
||||||
|
* @throws IllegalArgumentException 无法确定目标类型,且默认值为{@code null},无法确定类型
|
||||||
|
*/
|
||||||
|
Object convert(Type targetType, Object value);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user