diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java index 0499a70f3..b81710157 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/BeanToMapCopier.java @@ -44,6 +44,9 @@ public class BeanToMapCopier extends AbsCopier { actualEditable = copyOptions.editable; } + // 提前获取目标值真实类型 + final Type[] earlyDetectTypeArguments = TypeUtil.getTypeArguments(this.targetType); + final Map sourcePropDescMap = BeanUtil.getBeanDesc(actualEditable).getPropMap(copyOptions.ignoreCase); sourcePropDescMap.forEach((sFieldName, sDesc) -> { if (null == sFieldName || false == sDesc.isReadable(copyOptions.transientSupport)) { @@ -68,11 +71,10 @@ public class BeanToMapCopier extends AbsCopier { return; } - // 获取目标值真实类型并转换源值 - final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType); - if(null != typeArguments && typeArguments.length > 1){ + // 尝试转换源值 + if(null != earlyDetectTypeArguments && earlyDetectTypeArguments.length > 1){ //sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError); - sValue = this.copyOptions.convertField(typeArguments[1], sValue); + sValue = this.copyOptions.convertField(earlyDetectTypeArguments[1], sValue); } // 自定义值 diff --git a/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToMapCopier.java b/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToMapCopier.java index 500ddd73d..15ab71e61 100755 --- a/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToMapCopier.java +++ b/hutool-core/src/main/java/cn/hutool/core/bean/copier/MapToMapCopier.java @@ -33,6 +33,10 @@ public class MapToMapCopier extends AbsCopier { @Override public Map copy() { + + // 提前获取目标值真实类型 + final Type[] earlyDetectTypeArguments = TypeUtil.getTypeArguments(this.targetType); + this.source.forEach((sKey, sValue) -> { if (null == sKey) { return; @@ -57,11 +61,10 @@ public class MapToMapCopier extends AbsCopier { return; } - // 获取目标值真实类型并转换源值 - final Type[] typeArguments = TypeUtil.getTypeArguments(this.targetType); - if (null != typeArguments) { + // 尝试转换源值 + if (null != earlyDetectTypeArguments) { //sValue = Convert.convertWithCheck(typeArguments[1], sValue, null, this.copyOptions.ignoreError); - sValue = this.copyOptions.convertField(typeArguments[1], sValue); + sValue = this.copyOptions.convertField(earlyDetectTypeArguments[1], sValue); } // 自定义值