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