1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.johnzon.mapper.access; |
20 | |
|
21 | |
import org.apache.johnzon.mapper.MapperException; |
22 | |
|
23 | |
import java.beans.IntrospectionException; |
24 | |
import java.beans.Introspector; |
25 | |
import java.beans.PropertyDescriptor; |
26 | |
import java.lang.annotation.Annotation; |
27 | |
import java.lang.reflect.Method; |
28 | |
import java.lang.reflect.Type; |
29 | |
import java.util.HashMap; |
30 | |
import java.util.Map; |
31 | |
|
32 | 48 | public class MethodAccessMode implements AccessMode { |
33 | |
@Override |
34 | |
public Map<String, Reader> findReaders(final Class<?> clazz) { |
35 | 41 | final Map<String, Reader> readers = new HashMap<String, Reader>(); |
36 | 41 | final PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz); |
37 | 252 | for (final PropertyDescriptor descriptor : propertyDescriptors) { |
38 | 211 | final Method readMethod = descriptor.getReadMethod(); |
39 | 211 | if (readMethod != null && readMethod.getDeclaringClass() != Object.class) { |
40 | 169 | if (isIgnored(descriptor.getName())) { |
41 | 0 | continue; |
42 | |
} |
43 | 169 | readers.put(descriptor.getName(), new MethodReader(readMethod)); |
44 | |
} |
45 | |
} |
46 | 41 | return readers; |
47 | |
} |
48 | |
|
49 | |
@Override |
50 | |
public Map<String, Writer> findWriters(final Class<?> clazz) { |
51 | 41 | final Map<String, Writer> writers = new HashMap<String, Writer>(); |
52 | 41 | final PropertyDescriptor[] propertyDescriptors = getPropertyDescriptors(clazz); |
53 | 252 | for (final PropertyDescriptor descriptor : propertyDescriptors) { |
54 | 211 | final Method writeMethod = descriptor.getWriteMethod(); |
55 | 211 | if (writeMethod != null && writeMethod.getDeclaringClass() != Object.class) { |
56 | 163 | if (isIgnored(descriptor.getName())) { |
57 | 0 | continue; |
58 | |
} |
59 | 163 | writers.put(descriptor.getName(), new MethodWriter(writeMethod)); |
60 | |
} |
61 | |
} |
62 | 41 | return writers; |
63 | |
} |
64 | |
|
65 | |
protected boolean isIgnored(final String name) { |
66 | 332 | return name.equals("metaClass") || name.contains("$"); |
67 | |
} |
68 | |
|
69 | |
private PropertyDescriptor[] getPropertyDescriptors(final Class<?> clazz) { |
70 | |
final PropertyDescriptor[] propertyDescriptors; |
71 | |
try { |
72 | 82 | propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); |
73 | 0 | } catch (final IntrospectionException e) { |
74 | 0 | throw new IllegalStateException(e); |
75 | 82 | } |
76 | 82 | return propertyDescriptors; |
77 | |
} |
78 | |
|
79 | |
public static abstract class MethodDecoratedType implements DecoratedType { |
80 | |
public final Method method; |
81 | |
|
82 | 332 | public MethodDecoratedType(final Method method) { |
83 | 332 | this.method = method; |
84 | 332 | if (!method.isAccessible()) { |
85 | 80 | method.setAccessible(true); |
86 | |
} |
87 | 332 | } |
88 | |
|
89 | |
@Override |
90 | |
public <T extends Annotation> T getAnnotation(final Class<T> clazz) { |
91 | 668 | return method.getAnnotation(clazz); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
public static class MethodWriter extends MethodDecoratedType implements Writer { |
96 | |
public MethodWriter(final Method method) { |
97 | 163 | super(method); |
98 | 163 | } |
99 | |
|
100 | |
@Override |
101 | |
public Type getType() { |
102 | 163 | return method.getGenericParameterTypes()[0]; |
103 | |
} |
104 | |
|
105 | |
@Override |
106 | |
public void write(final Object instance, final Object value) { |
107 | |
try { |
108 | 112 | method.invoke(instance, value); |
109 | 0 | } catch (final Exception e) { |
110 | 0 | throw new MapperException(e); |
111 | 112 | } |
112 | 112 | } |
113 | |
} |
114 | |
|
115 | 48 | public static class MethodReader extends MethodDecoratedType implements Reader { |
116 | |
public MethodReader(final Method method) { |
117 | 169 | super(method); |
118 | 169 | } |
119 | |
|
120 | |
@Override |
121 | |
public Type getType() { |
122 | 507 | return method.getGenericReturnType(); |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public Object read(final Object instance) { |
127 | |
try { |
128 | 131 | return method.invoke(instance); |
129 | 0 | } catch (final Exception e) { |
130 | 0 | throw new MapperException(e); |
131 | |
} |
132 | |
} |
133 | |
} |
134 | |
} |