mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-05-31 18:03:52 +08:00
优化数组转换逻辑
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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<String,Object> 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()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -64,6 +64,8 @@ public class Target {
|
||||
|
||||
private Integer[] arr4;
|
||||
|
||||
private Color[] colors;
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
Reference in New Issue
Block a user