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.JohnzonProperty; |
22 | |
import org.apache.johnzon.mapper.MapperException; |
23 | |
|
24 | |
import java.lang.annotation.Annotation; |
25 | |
import java.lang.reflect.Field; |
26 | |
import java.lang.reflect.Modifier; |
27 | |
import java.lang.reflect.Type; |
28 | |
import java.util.HashMap; |
29 | |
import java.util.Map; |
30 | |
|
31 | 4 | public class FieldAccessMode implements AccessMode { |
32 | |
@Override |
33 | |
public Map<String, Reader> findReaders(final Class<?> clazz) { |
34 | 4 | final Map<String, Reader> readers = new HashMap<String, Reader>(); |
35 | 4 | for (final Map.Entry<String, Field> f : fields(clazz).entrySet()) { |
36 | 7 | final String key = f.getKey(); |
37 | 7 | if (isIgnored(key)) { |
38 | 0 | continue; |
39 | |
} |
40 | |
|
41 | 7 | readers.put(extractKey(f.getValue(), key), new FieldReader(f.getValue())); |
42 | 7 | } |
43 | 4 | return readers; |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
public Map<String, Writer> findWriters(final Class<?> clazz) { |
48 | 4 | final Map<String, Writer> writers = new HashMap<String, Writer>(); |
49 | 4 | for (final Map.Entry<String, Field> f : fields(clazz).entrySet()) { |
50 | 7 | final String key = f.getKey(); |
51 | 7 | if (isIgnored(key)) { |
52 | 0 | continue; |
53 | |
} |
54 | 7 | writers.put(extractKey(f.getValue(), key), new FieldWriter(f.getValue())); |
55 | 7 | } |
56 | 4 | return writers; |
57 | |
} |
58 | |
|
59 | |
private String extractKey(final Field f, final String key) { |
60 | 14 | final JohnzonProperty property = f.getAnnotation(JohnzonProperty.class); |
61 | 14 | return property != null ? property.value() : key; |
62 | |
} |
63 | |
|
64 | |
protected boolean isIgnored(final String key) { |
65 | 14 | return key.contains("$"); |
66 | |
} |
67 | |
|
68 | |
private Map<String, Field> fields(final Class<?> clazz) { |
69 | 8 | final Map<String, Field> fields = new HashMap<String, Field>(); |
70 | 8 | Class<?> current = clazz; |
71 | 18 | while (current != null && current != Object.class) { |
72 | 24 | for (final Field f : current.getDeclaredFields()) { |
73 | 14 | final String name = f.getName(); |
74 | 14 | final int modifiers = f.getModifiers(); |
75 | 14 | if (fields.containsKey(name) |
76 | |
|| Modifier.isStatic(modifiers) |
77 | |
|| Modifier.isTransient(modifiers)) { |
78 | 0 | continue; |
79 | |
} |
80 | 14 | fields.put(name, f); |
81 | |
} |
82 | 10 | current = current.getSuperclass(); |
83 | |
} |
84 | 8 | return fields; |
85 | |
} |
86 | |
|
87 | |
protected static abstract class FieldDecoratedType implements DecoratedType { |
88 | |
protected final Field field; |
89 | |
|
90 | 14 | public FieldDecoratedType(final Field field) { |
91 | 14 | this.field = field; |
92 | 14 | if (!field.isAccessible()) { |
93 | 14 | this.field.setAccessible(true); |
94 | |
} |
95 | 14 | } |
96 | |
|
97 | |
@Override |
98 | |
public Type getType() { |
99 | 28 | return field.getGenericType(); |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public <T extends Annotation> T getAnnotation(final Class<T> clazz) { |
104 | 28 | return field.getAnnotation(clazz); |
105 | |
} |
106 | |
} |
107 | |
|
108 | 21 | public static class FieldWriter extends FieldDecoratedType implements Writer { |
109 | |
public FieldWriter(final Field field) { |
110 | 7 | super(field); |
111 | 7 | } |
112 | |
|
113 | |
@Override |
114 | |
public void write(final Object instance, final Object value) { |
115 | |
try { |
116 | 6 | field.set(instance, value); |
117 | 0 | } catch (final Exception e) { |
118 | 0 | throw new MapperException(e); |
119 | 6 | } |
120 | 6 | } |
121 | |
} |
122 | |
|
123 | 35 | public static class FieldReader extends FieldDecoratedType implements Reader { |
124 | |
public FieldReader(final Field field) { |
125 | 7 | super(field); |
126 | 7 | } |
127 | |
|
128 | |
@Override |
129 | |
public Object read(final Object instance) { |
130 | |
try { |
131 | 5 | return field.get(instance); |
132 | 0 | } catch (final Exception e) { |
133 | 0 | throw new MapperException(e); |
134 | |
} |
135 | |
} |
136 | |
} |
137 | |
} |