Skip to content

Commit fc5e18d

Browse files
committed
修复无法复制代理对象问题
1 parent 60dd787 commit fc5e18d

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

hsweb-core/src/main/java/org/hswebframework/web/bean/FastBeanCopier.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,23 @@ public static <T, S> T copy(S source, T target, Converter converter, Set<String>
114114
return target;
115115
}
116116

117+
static Class getUserClass(Object object) {
118+
if (object instanceof Map) {
119+
return Map.class;
120+
}
121+
Class type = ClassUtils.getUserClass(object);
122+
123+
if (java.lang.reflect.Proxy.isProxyClass(type)) {
124+
Class[] interfaces= type.getInterfaces();
125+
return interfaces[0];
126+
}
127+
128+
return type;
129+
}
130+
117131
public static Copier getCopier(Object source, Object target, boolean autoCreate) {
118-
Class sourceType = source instanceof Map ? Map.class : ClassUtils.getUserClass(source);
119-
Class targetType = target instanceof Map ? Map.class : ClassUtils.getUserClass(target);
132+
Class sourceType = getUserClass(source);
133+
Class targetType = getUserClass(target);
120134
CacheKey key = createCacheKey(sourceType, targetType);
121135
if (autoCreate) {
122136
return CACHE.computeIfAbsent(key, k -> createCopier(sourceType, targetType));
@@ -541,7 +555,7 @@ public <T> T convert(Object source, Class<T> targetClass, Class[] genericType) {
541555

542556
if (targetClass.isEnum()) {
543557
if (EnumDict.class.isAssignableFrom(targetClass)) {
544-
String strVal=String.valueOf(source);
558+
String strVal = String.valueOf(source);
545559

546560
Object val = EnumDict.find((Class) targetClass, e -> {
547561
return e.eq(source) || e.name().equalsIgnoreCase(strVal);
@@ -564,7 +578,7 @@ public <T> T convert(Object source, Class<T> targetClass, Class[] genericType) {
564578
if (targetClass.isArray()) {
565579
Class<?> componentType = targetClass.getComponentType();
566580
List<?> val = convert(source, List.class, new Class[]{componentType});
567-
return (T) val.toArray((Object[])Array.newInstance(componentType,val.size()));
581+
return (T) val.toArray((Object[]) Array.newInstance(componentType, val.size()));
568582
}
569583

570584
try {

hsweb-core/src/test/java/org/hswebframework/web/bean/FastBeanCopierTest.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package org.hswebframework.web.bean;
22

3-
import org.apache.commons.beanutils.BeanUtils;
43
import org.junit.Assert;
54
import org.junit.Test;
65

76
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Proxy;
88
import java.util.Arrays;
99
import java.util.Collections;
1010
import java.util.HashMap;
1111
import java.util.Map;
12+
import java.util.concurrent.atomic.AtomicReference;
1213

1314
/**
1415
* @author zhouhao
@@ -44,8 +45,8 @@ public void test() throws InvocationTargetException, IllegalAccessException {
4445

4546
@Test
4647
public void testMapArray() {
47-
Map<String,Object> data =new HashMap<>();
48-
data.put("colors", Arrays.asList("RED"));
48+
Map<String, Object> data = new HashMap<>();
49+
data.put("colors", Arrays.asList("RED"));
4950

5051

5152
Target target = new Target();
@@ -82,4 +83,41 @@ public void testCopyMap() {
8283
}
8384

8485

86+
@Test
87+
public void testProxy() {
88+
AtomicReference<Object> reference=new AtomicReference<>();
89+
90+
ProxyTest test = (ProxyTest) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
91+
new Class[]{ProxyTest.class}, (proxy, method, args) -> {
92+
if (method.getName().equals("getName")) {
93+
return "test";
94+
}
95+
96+
if (method.getName().equals("setName")) {
97+
reference.set(args[0]);
98+
return null;
99+
}
100+
101+
return null;
102+
});
103+
104+
Target source = new Target();
105+
106+
FastBeanCopier.copy(test,source);
107+
Assert.assertEquals(source.getName(),test.getName());
108+
109+
110+
source.setName("test2");
111+
FastBeanCopier.copy(source,test);
112+
113+
Assert.assertEquals(reference.get(),source.getName());
114+
}
115+
116+
117+
public interface ProxyTest {
118+
String getName();
119+
120+
void setName(String name);
121+
}
122+
85123
}

0 commit comments

Comments
 (0)