1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.dozer.propertydescriptor; |
17 | |
|
18 | |
import org.dozer.fieldmap.HintContainer; |
19 | |
import org.dozer.util.MappingUtils; |
20 | |
import org.dozer.util.ReflectionUtils; |
21 | |
|
22 | |
import java.lang.ref.SoftReference; |
23 | |
import java.lang.reflect.Method; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class CustomGetSetPropertyDescriptor extends JavaBeanPropertyDescriptor { |
35 | |
|
36 | |
private final String customSetMethod; |
37 | |
private final String customGetMethod; |
38 | |
|
39 | |
private SoftReference<Method> writeMethod; |
40 | |
private SoftReference<Method> readMethod; |
41 | |
|
42 | |
public CustomGetSetPropertyDescriptor(Class<?> clazz, String fieldName, boolean isIndexed, int index, String customSetMethod, |
43 | |
String customGetMethod, HintContainer srcDeepIndexHintContainer, HintContainer destDeepIndexHintContainer) { |
44 | 2449 | super(clazz, fieldName, isIndexed, index, srcDeepIndexHintContainer, destDeepIndexHintContainer); |
45 | 2449 | this.customSetMethod = customSetMethod; |
46 | 2449 | this.customGetMethod = customGetMethod; |
47 | 2449 | } |
48 | |
|
49 | |
@Override |
50 | |
public Method getWriteMethod() throws NoSuchMethodException { |
51 | 2488 | if (writeMethod == null || writeMethod.get() == null) { |
52 | 2423 | if (customSetMethod != null && !MappingUtils.isDeepMapping(fieldName)) { |
53 | 2311 | Method method = ReflectionUtils.findAMethod(clazz, customSetMethod); |
54 | 2311 | writeMethod = new SoftReference<Method>(method); |
55 | 2311 | } else { |
56 | 112 | return super.getWriteMethod(); |
57 | |
} |
58 | |
} |
59 | 2376 | return writeMethod.get(); |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
protected Method getReadMethod() throws NoSuchMethodException { |
64 | 2524 | if (readMethod == null || readMethod.get() == null) { |
65 | 2515 | if (customGetMethod != null) { |
66 | 547 | Method method = ReflectionUtils.findAMethod(clazz, customGetMethod); |
67 | 545 | readMethod = new SoftReference<Method>(method); |
68 | 545 | } else { |
69 | 1968 | return super.getReadMethod(); |
70 | |
} |
71 | |
} |
72 | 554 | return readMethod.get(); |
73 | |
} |
74 | |
|
75 | |
@Override |
76 | |
protected String getSetMethodName() throws NoSuchMethodException { |
77 | 14 | return customSetMethod != null ? customSetMethod : super.getSetMethodName(); |
78 | |
} |
79 | |
|
80 | |
@Override |
81 | |
protected boolean isCustomSetMethod() { |
82 | 14 | return customSetMethod != null; |
83 | |
} |
84 | |
|
85 | |
} |