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 00dfc7514..7c6c78d58 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 @@ -14,6 +14,7 @@ import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; import java.beans.PropertyDescriptor; +import java.lang.reflect.Array; import java.lang.reflect.Field; import java.util.*; import java.util.function.BiFunction; @@ -540,7 +541,11 @@ public final class FastBeanCopier { if (targetClass.isEnum()) { if (EnumDict.class.isAssignableFrom(targetClass)) { - Object val = EnumDict.find((Class) targetClass, String.valueOf(source)).orElse(null); + String strVal=String.valueOf(source); + + Object val = EnumDict.find((Class) targetClass, e -> { + return e.eq(source) || e.name().equalsIgnoreCase(strVal); + }).orElse(null); if (targetClass.isInstance(val)) { return ((T) val); } @@ -551,9 +556,17 @@ public final class FastBeanCopier { return t; } } + log.warn("无法将:{}转为枚举:{}", source, targetClass); return null; } + //转换为数组 + if (targetClass.isArray()) { + Class componentType = targetClass.getComponentType(); + List val = convert(source, List.class, new Class[]{componentType}); + return (T) val.toArray((Object[])Array.newInstance(componentType,val.size())); + } + try { org.apache.commons.beanutils.Converter converter = BeanUtilsBean .getInstance() diff --git a/hsweb-core/src/test/java/org/hswebframework/web/bean/FastBeanCopierTest.java b/hsweb-core/src/test/java/org/hswebframework/web/bean/FastBeanCopierTest.java index 4b37efd62..defe37706 100644 --- a/hsweb-core/src/test/java/org/hswebframework/web/bean/FastBeanCopierTest.java +++ b/hsweb-core/src/test/java/org/hswebframework/web/bean/FastBeanCopierTest.java @@ -1,9 +1,11 @@ package org.hswebframework.web.bean; import org.apache.commons.beanutils.BeanUtils; +import org.junit.Assert; import org.junit.Test; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -35,35 +37,26 @@ public class FastBeanCopierTest { FastBeanCopier.copy(source, target); - long t = System.currentTimeMillis(); -// for (int i = 10_0000; i > 0; i--) { -// FastBeanCopier.copy(source, target); -// } - System.out.println(System.currentTimeMillis() - t); - System.out.println(source); System.out.println(target); System.out.println(target.getNestObject() == source.getNestObject()); -// Source source1=new Source(); - -// FastBeanCopier.copy(source,source1); - -// System.out.println(source1); -// -// t = System.currentTimeMillis(); -// -// for (int i = 100_0000; i > 0; i--) { -// try { -// BeanUtils.copyProperties(source, target); -// } catch (Exception e) { -// e.printStackTrace(); -// } -// } -// System.out.println(System.currentTimeMillis() - t); -// System.out.println(target); -// System.out.println(target.getNestObject() == source.getNestObject()); } + @Test + public void testMapArray() { + Map data =new HashMap<>(); + data.put("colors", Arrays.asList("RED")); + + + Target target = new Target(); + FastBeanCopier.copy(data, target); + + + System.out.println(target); + Assert.assertNotNull(target.getColors()); + Assert.assertSame(target.getColors()[0], Color.RED); + + } @Test public void testCopyMap() { @@ -85,7 +78,7 @@ public class FastBeanCopierTest { System.out.println(FastBeanCopier.copy(source, target, FastBeanCopier.include("age"))); System.out.println(target); - System.out.println(FastBeanCopier.copy(target, new Source())); + System.out.println(FastBeanCopier.copy(target, new Target())); } diff --git a/hsweb-core/src/test/java/org/hswebframework/web/bean/Source.java b/hsweb-core/src/test/java/org/hswebframework/web/bean/Source.java index 872891df2..67aa5fb5d 100644 --- a/hsweb-core/src/test/java/org/hswebframework/web/bean/Source.java +++ b/hsweb-core/src/test/java/org/hswebframework/web/bean/Source.java @@ -55,6 +55,8 @@ public class Source { private String[] arr6 = {"1", "2"}; + private Color[] colors ={Color.BLUE,Color.RED}; + private String source; private String target; diff --git a/hsweb-core/src/test/java/org/hswebframework/web/bean/Target.java b/hsweb-core/src/test/java/org/hswebframework/web/bean/Target.java index f41a3f667..8b50678c9 100644 --- a/hsweb-core/src/test/java/org/hswebframework/web/bean/Target.java +++ b/hsweb-core/src/test/java/org/hswebframework/web/bean/Target.java @@ -64,6 +64,8 @@ public class Target { private Integer[] arr4; + private Color[] colors; + @Override public String toString() {