优化XXXToMapCopier的部分性能(pr#1345@Gitee)

This commit is contained in:
Looly
2025-05-23 21:54:13 +08:00
parent 596cdc2984
commit 3d3cadd1bc
2 changed files with 8 additions and 19 deletions

View File

@@ -33,10 +33,7 @@ import java.util.Map;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public class BeanToMapCopier extends AbsCopier<Object, Map> { public class BeanToMapCopier extends AbsCopier<Object, Map> {
/** private final Type[] targetTypeArguments;
* 目标的Map类型用于泛型类注入
*/
private final Type targetType;
/** /**
* 构造 * 构造
@@ -48,7 +45,7 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
*/ */
public BeanToMapCopier(final Object source, final Map target, final Type targetType, final CopyOptions copyOptions) { public BeanToMapCopier(final Object source, final Map target, final Type targetType, final CopyOptions copyOptions) {
super(source, target, copyOptions); super(source, target, copyOptions);
this.targetType = targetType; targetTypeArguments = TypeUtil.getTypeArguments(targetType);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@@ -63,9 +60,6 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
actualEditable = copyOptions.editable; actualEditable = copyOptions.editable;
} }
// 提前获取目标值真实类型
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
final Map<String, PropDesc> sourcePropDescMap = getBeanDesc(actualEditable).getPropMap(copyOptions.ignoreCase); final Map<String, PropDesc> sourcePropDescMap = getBeanDesc(actualEditable).getPropMap(copyOptions.ignoreCase);
sourcePropDescMap.forEach((sFieldName, sDesc) -> { sourcePropDescMap.forEach((sFieldName, sDesc) -> {
if (null == sFieldName || !sDesc.isReadable(copyOptions.transientSupport)) { if (null == sFieldName || !sDesc.isReadable(copyOptions.transientSupport)) {
@@ -93,9 +87,9 @@ public class BeanToMapCopier extends AbsCopier<Object, Map> {
// 获取目标值真实类型并转换源值 // 获取目标值真实类型并转换源值
// 尝试转换源值 // 尝试转换源值
if(null != typeArguments && typeArguments.length > 1){ if(null != targetTypeArguments && targetTypeArguments.length > 1){
//sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError); //sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError);
sValue = copyOptions.convertField(typeArguments[1], sValue); sValue = copyOptions.convertField(targetTypeArguments[1], sValue);
} }
// 目标赋值 // 目标赋值

View File

@@ -30,10 +30,7 @@ import java.util.Map;
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
public class MapToMapCopier extends AbsCopier<Map, Map> { public class MapToMapCopier extends AbsCopier<Map, Map> {
/** private final Type[] targetTypeArguments;
* 目标的类型(用于泛型类注入)
*/
private final Type targetType;
/** /**
* 构造 * 构造
@@ -45,13 +42,11 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
*/ */
public MapToMapCopier(final Map source, final Map target, final Type targetType, final CopyOptions copyOptions) { public MapToMapCopier(final Map source, final Map target, final Type targetType, final CopyOptions copyOptions) {
super(source, target, copyOptions); super(source, target, copyOptions);
this.targetType = targetType; targetTypeArguments = TypeUtil.getTypeArguments(targetType);
} }
@Override @Override
public Map copy() { public Map copy() {
final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType);
this.source.forEach((sKey, sValue) -> { this.source.forEach((sKey, sValue) -> {
if (null == sKey) { if (null == sKey) {
return; return;
@@ -76,8 +71,8 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
} }
// 获取目标值真实类型并转换源值 // 获取目标值真实类型并转换源值
if(null != typeArguments){ if(null != targetTypeArguments){
sValue = this.copyOptions.convertField(typeArguments[1], sValue); sValue = this.copyOptions.convertField(targetTypeArguments[1], sValue);
} }
// 忽略空值 // 忽略空值