From 0a5cef3973aa7b3cc80e691656fd9b0ae478d133 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Thu, 19 Dec 2024 09:36:58 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=BD=AC=E6=8D=A2=E5=BC=82=E5=B8=B8=E6=A0=88=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/bean/FastBeanCopier.java | 73 ++++++++++--------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java b/hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java index 0e331dd9e..bbe7e0d92 100644 --- a/hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java +++ b/hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java @@ -126,17 +126,17 @@ public final class FastBeanCopier { ((Map) target).putAll(((Map) source)); } else { ((Map) source) - .forEach((k, v) -> { - if (!ignore.contains(k)) { - ((Map) target).put(k, v); - } - }); + .forEach((k, v) -> { + if (!ignore.contains(k)) { + ((Map) target).put(k, v); + } + }); } return target; } getCopier(source, target, true) - .copy(source, target, ignore, converter); + .copy(source, target, ignore, converter); return target; } @@ -180,20 +180,20 @@ public final class FastBeanCopier { tartName = tartName.substring("package ".length()); } String method = "public void copy(Object s, Object t, java.util.Set ignore, " + - "org.hswebframework.web.bean.Converter converter){\n" + - "try{\n\t" + - sourceName + " $$__source=(" + sourceName + ")s;\n\t" + - tartName + " $$__target=(" + tartName + ")t;\n\t" + - createCopierCode(source, target) + - "}catch(Throwable e){\n" + - "\tthrow e;" + - "\n}\n" + - "\n}"; + "org.hswebframework.web.bean.Converter converter){\n" + + "try{\n\t" + + sourceName + " $$__source=(" + sourceName + ")s;\n\t" + + tartName + " $$__target=(" + tartName + ")t;\n\t" + + createCopierCode(source, target) + + "}catch(Throwable e){\n" + + "\tthrow e;" + + "\n}\n" + + "\n}"; try { @SuppressWarnings("all") Proxy proxy = Proxy - .create(Copier.class, new Class[]{source, target}) - .addMethod(method); + .create(Copier.class, new Class[]{source, target}) + .addMethod(method); return proxy.newInstance(); } catch (Exception e) { log.error("创建bean copy 代理对象失败:\n{}", method, e); @@ -208,8 +208,8 @@ public final class FastBeanCopier { return Stream.of(propertyUtils.getPropertyDescriptors(type)) .filter(property -> !property - .getName() - .equals("class") && property.getReadMethod() != null && property.getWriteMethod() != null) + .getName() + .equals("class") && property.getReadMethod() != null && property.getWriteMethod() != null) .map(BeanClassProperty::new) //让字段有序 .sorted(Comparator.comparing(property -> fieldNames.indexOf(property.name))) @@ -266,9 +266,9 @@ public final class FastBeanCopier { code.append("\tif(").append(sourceProperty.getName()).append("!=null){\n"); } code - .append("\t$$__target.") - .append(targetProperty.generateSetter(targetProperty.getType(), sourceProperty.getName())) - .append(";\n"); + .append("\t$$__target.") + .append(targetProperty.generateSetter(targetProperty.getType(), sourceProperty.getName())) + .append(";\n"); if (!targetProperty.isPrimitive()) { code.append("\t}\n"); } @@ -376,7 +376,7 @@ public final class FastBeanCopier { } } String convert = "converter.convert((Object)(" + (isPrimitive() ? castWrapper(getterCode) : getterCode) + ")," - + getTypeName(targetType) + ".class," + generic + ")"; + + getTypeName(targetType) + ".class," + generic + ")"; StringBuilder convertCode = new StringBuilder(); if (targetType != getType()) { @@ -389,10 +389,10 @@ public final class FastBeanCopier { // source.getField().intValue(); if (sourceIsWrapper) { convertCode - .append(getterCode) - .append(".") - .append(sourcePrimitive.getName()) - .append("Value()"); + .append(getterCode) + .append(".") + .append(sourcePrimitive.getName()) + .append("Value()"); } else { //类型不一致,调用convert转换 convertCode.append("((").append(targetWrapperClass.getName()) @@ -427,17 +427,17 @@ public final class FastBeanCopier { if (Cloneable.class.isAssignableFrom(targetType)) { try { convertCode - .append("(") - .append(getTypeName()) - .append(")") - .append(getterCode) - .append(".clone()"); + .append("(") + .append(getTypeName()) + .append(")") + .append(getterCode) + .append(".clone()"); } catch (Exception e) { convertCode.append(getterCode); } } else { if ((Map.class.isAssignableFrom(targetType) - || Collection.class.isAssignableFrom(type)) && hasGeneric) { + || Collection.class.isAssignableFrom(type)) && hasGeneric) { convertCode.append("(").append(getTypeName()).append(")").append(convert); } else { convertCode.append("(").append(getTypeName()).append(")").append(getterCode); @@ -625,12 +625,15 @@ public final class FastBeanCopier { for (Object e : target.getEnums()) { Enum t = ((Enum) e); if ((t.name().equalsIgnoreCase(strSource) - || Objects.equals(String.valueOf(t.ordinal()), strSource))) { + || Objects.equals(String.valueOf(t.ordinal()), strSource))) { return (T) e; } } - log.warn("无法将:{}转为枚举:{}", source, targetClass); + log.warn("无法将:{}转为枚举:{}", + source, + targetClass, + new ClassCastException(source + "=>" + targetClass)); return null; } //转换为数组