|
@@ -1,23 +1,17 @@
|
|
-package org.hswebframework.web.boost.bean;
|
|
|
|
|
|
+package org.hswebframework.web.bean;
|
|
|
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
-import lombok.Setter;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.beanutils.BeanUtilsBean;
|
|
import org.apache.commons.beanutils.BeanUtilsBean;
|
|
import org.apache.commons.beanutils.PropertyUtilsBean;
|
|
import org.apache.commons.beanutils.PropertyUtilsBean;
|
|
-import org.hswebframework.web.boost.Compiler;
|
|
|
|
|
|
+import org.hswebframework.web.dict.EnumDict;
|
|
|
|
+import org.hswebframework.web.proxy.Proxy;
|
|
import org.springframework.util.ClassUtils;
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
|
-import java.beans.BeanDescriptor;
|
|
|
|
-import java.beans.FeatureDescriptor;
|
|
|
|
import java.beans.PropertyDescriptor;
|
|
import java.beans.PropertyDescriptor;
|
|
-import java.lang.reflect.Method;
|
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
-import java.util.concurrent.ConcurrentMap;
|
|
|
|
import java.util.function.BiFunction;
|
|
import java.util.function.BiFunction;
|
|
-import java.util.function.Consumer;
|
|
|
|
import java.util.function.Function;
|
|
import java.util.function.Function;
|
|
import java.util.function.Supplier;
|
|
import java.util.function.Supplier;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -28,7 +22,7 @@ import java.util.stream.Stream;
|
|
* @since 3.0
|
|
* @since 3.0
|
|
*/
|
|
*/
|
|
@Slf4j
|
|
@Slf4j
|
|
-public class FastBeanCopier {
|
|
|
|
|
|
+public final class FastBeanCopier {
|
|
private static final Map<CacheKey, Copier> CACHE = new HashMap<>();
|
|
private static final Map<CacheKey, Copier> CACHE = new HashMap<>();
|
|
|
|
|
|
private static final PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
|
|
private static final PropertyUtilsBean propertyUtils = BeanUtilsBean.getInstance().getPropertyUtils();
|
|
@@ -92,7 +86,7 @@ public class FastBeanCopier {
|
|
|
|
|
|
public static Copier createCopier(Class source, Class target) {
|
|
public static Copier createCopier(Class source, Class target) {
|
|
String method = "public void copy(Object s, Object t, java.util.Set ignore, " +
|
|
String method = "public void copy(Object s, Object t, java.util.Set ignore, " +
|
|
- "org.hswebframework.web.boost.bean.Converter converter){\n" +
|
|
|
|
|
|
+ "org.hswebframework.web.bean.Converter converter){\n" +
|
|
"try{\n\t" +
|
|
"try{\n\t" +
|
|
source.getName() + " source=(" + source.getName() + ")s;\n\t" +
|
|
source.getName() + " source=(" + source.getName() + ")s;\n\t" +
|
|
target.getName() + " target=(" + target.getName() + ")t;\n\t" +
|
|
target.getName() + " target=(" + target.getName() + ")t;\n\t" +
|
|
@@ -102,7 +96,8 @@ public class FastBeanCopier {
|
|
"\n}\n" +
|
|
"\n}\n" +
|
|
"\n}";
|
|
"\n}";
|
|
try {
|
|
try {
|
|
- return Compiler.create(Copier.class)
|
|
|
|
|
|
+ System.out.println(method);
|
|
|
|
+ return Proxy.create(Copier.class)
|
|
.addMethod(method)
|
|
.addMethod(method)
|
|
.newInstance();
|
|
.newInstance();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -371,25 +366,7 @@ public class FastBeanCopier {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- static class DefaultConvert implements Converter {
|
|
|
|
-// static {
|
|
|
|
-// BeanUtilsBean
|
|
|
|
-// .getInstance()
|
|
|
|
-// .getConvertUtils().register(new org.apache.commons.beanutils.Converter() {
|
|
|
|
-// @Override
|
|
|
|
-// public <T> T convert(Class<T> type, Object value) {
|
|
|
|
-// if (value == null) {
|
|
|
|
-// return null;
|
|
|
|
-// }
|
|
|
|
-// if (value instanceof Collection) {
|
|
|
|
-// return (T) new ArrayList(((Collection) value));
|
|
|
|
-// } else if (value.getClass().isArray()) {
|
|
|
|
-// return (T) new ArrayList(Arrays.asList(((Object[]) value)));
|
|
|
|
-// }
|
|
|
|
-// return null;
|
|
|
|
-// }
|
|
|
|
-// }, List.class);
|
|
|
|
-// }
|
|
|
|
|
|
+ static final class DefaultConvert implements Converter {
|
|
|
|
|
|
public Collection newCollection(Class targetClass) {
|
|
public Collection newCollection(Class targetClass) {
|
|
|
|
|
|
@@ -409,10 +386,20 @@ public class FastBeanCopier {
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
|
+ @SuppressWarnings("all")
|
|
public <T> T convert(Object source, Class<T> targetClass) {
|
|
public <T> T convert(Object source, Class<T> targetClass) {
|
|
if (source == null) {
|
|
if (source == null) {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
+ if (source.getClass().isEnum()) {
|
|
|
|
+ if (source instanceof EnumDict) {
|
|
|
|
+ Object val = (T) ((EnumDict) source).getValue();
|
|
|
|
+ if (targetClass.isInstance(val)) {
|
|
|
|
+ return ((T) val);
|
|
|
|
+ }
|
|
|
|
+ return convert(val, targetClass);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (targetClass == String.class) {
|
|
if (targetClass == String.class) {
|
|
return (T) String.valueOf(source);
|
|
return (T) String.valueOf(source);
|
|
}
|
|
}
|
|
@@ -444,6 +431,14 @@ public class FastBeanCopier {
|
|
}
|
|
}
|
|
|
|
|
|
if (targetClass.isEnum()) {
|
|
if (targetClass.isEnum()) {
|
|
|
|
+ if (EnumDict.class.isAssignableFrom(targetClass)) {
|
|
|
|
+ Object val = EnumDict.find((Class) targetClass, String.valueOf(source)).orElse(null);
|
|
|
|
+
|
|
|
|
+ if (targetClass.isInstance(val)) {
|
|
|
|
+ return ((T) val);
|
|
|
|
+ }
|
|
|
|
+ return convert(val, targetClass);
|
|
|
|
+ }
|
|
for (T t : targetClass.getEnumConstants()) {
|
|
for (T t : targetClass.getEnumConstants()) {
|
|
if (((Enum) t).name().equalsIgnoreCase(String.valueOf(source))) {
|
|
if (((Enum) t).name().equalsIgnoreCase(String.valueOf(source))) {
|
|
return t;
|
|
return t;
|
|
@@ -454,7 +449,7 @@ public class FastBeanCopier {
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
|
|
|
|
- T newTarget = targetClass == Map.class ? (T)new HashMap<>() : targetClass.newInstance();
|
|
|
|
|
|
+ T newTarget = targetClass == Map.class ? (T) new HashMap<>() : targetClass.newInstance();
|
|
copy(source, newTarget);
|
|
copy(source, newTarget);
|
|
return newTarget;
|
|
return newTarget;
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|