forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionDecoder.java
More file actions
181 lines (168 loc) · 6.07 KB
/
ReflectionDecoder.java
File metadata and controls
181 lines (168 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.jsoniter;
import com.jsoniter.spi.*;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.*;
public class ReflectionDecoder implements Decoder {
private static Object NOT_SET = new Object() {
@Override
public String toString() {
return "NOT_SET";
}
};
private Constructor ctor;
private Method staticFactory;
private Map<String, Binding> allBindings = new HashMap<String, Binding>();
private List<Binding> ctorParams = new ArrayList<Binding>();
private ThreadLocal<Object[]> tempTls;
private ThreadLocal<Object[]> ctorArgsTls;
private List<Binding> fields;
private List<SetterDescriptor> setters;
public ReflectionDecoder(Class clazz) {
try {
init(clazz);
} catch (Exception e) {
throw new JsonException(e);
}
}
private final void init(Class clazz) throws Exception {
ClassDescriptor desc = ExtensionManager.getClassDescriptor(clazz, true);
ctorParams = desc.ctor.parameters;
int tempIdx = 0;
for (Binding param : ctorParams) {
tempIdx = addBinding(clazz, tempIdx, param);
}
this.ctor = desc.ctor.ctor;
this.staticFactory = desc.ctor.staticFactory;
fields = desc.fields;
for (Binding field : fields) {
tempIdx = addBinding(clazz, tempIdx, field);
}
setters = desc.setters;
for (SetterDescriptor setter : setters) {
for (Binding param : setter.parameters) {
tempIdx = addBinding(clazz, tempIdx, param);
}
}
if (!ctorParams.isEmpty() || !setters.isEmpty()) {
final int tempCount = tempIdx;
tempTls = new ThreadLocal<Object[]>() {
@Override
protected Object[] initialValue() {
return new Object[tempCount];
}
};
ctorArgsTls = new ThreadLocal<Object[]>() {
@Override
protected Object[] initialValue() {
return new Object[ctorParams.size()];
}
};
}
}
private int addBinding(Class clazz, int tempIdx, Binding param) {
param.idx = tempIdx;
for (String fromName : param.fromNames) {
if (allBindings.containsKey(fromName)) {
throw new JsonException("name conflict found in " + clazz +": " + fromName);
}
allBindings.put(fromName, param);
}
tempIdx++;
return tempIdx;
}
@Override
public final Object decode(JsonIterator iter) throws IOException {
try {
if (ctorParams.isEmpty()) {
if (setters.isEmpty()) {
return decodeWithOnlyFieldBinding(iter);
} else {
return decodeWithSetterBinding(iter);
}
} else {
return decodeWithCtorBinding(iter);
}
} catch (Exception e) {
throw new JsonException(e);
}
}
private final Object decodeWithCtorBinding(JsonIterator iter) throws Exception {
Object[] temp = tempTls.get();
Arrays.fill(temp, NOT_SET);
for (String fieldName = iter.readObject(); fieldName != null; fieldName = iter.readObject()) {
Binding binding = allBindings.get(fieldName);
if (binding == null) {
iter.skip();
continue;
}
temp[binding.idx] = iter.read(binding.valueTypeLiteral);
}
Object[] ctorArgs = ctorArgsTls.get();
Arrays.fill(ctorArgs, null);
for (int i = 0; i < ctorParams.size(); i++) {
Object arg = temp[ctorParams.get(i).idx];
if (arg != NOT_SET) {
ctorArgs[i] = arg;
}
}
Object obj = createNewObject(ctorArgs);
for (Binding field : fields) {
Object val = temp[field.idx];
if (val != NOT_SET) {
field.field.set(obj, val);
}
}
applySetters(temp, obj);
return obj;
}
private void applySetters(Object[] temp, Object obj) throws Exception {
for (SetterDescriptor setter : setters) {
Object[] args = new Object[setter.parameters.size()];
for (int i = 0; i < setter.parameters.size(); i++) {
args[i] = temp[setter.parameters.get(i).idx];
}
setter.method.invoke(obj, args);
}
}
private final Object decodeWithOnlyFieldBinding(JsonIterator iter) throws Exception {
Object obj = createNewObject();
for (String fieldName = iter.readObject(); fieldName != null; fieldName = iter.readObject()) {
Binding binding = allBindings.get(fieldName);
if (binding == null) {
iter.skip();
continue;
}
// TODO: when setter is single argument, decode like field
binding.field.set(obj, iter.read(binding.valueTypeLiteral));
}
return obj;
}
private final Object decodeWithSetterBinding(JsonIterator iter) throws Exception {
Object[] temp = tempTls.get();
Arrays.fill(temp, null);
Object obj = createNewObject();
for (String fieldName = iter.readObject(); fieldName != null; fieldName = iter.readObject()) {
Binding binding = allBindings.get(fieldName);
if (binding == null) {
iter.skip();
continue;
}
if (binding.field == null) {
temp[binding.idx] = iter.read(binding.valueTypeLiteral);
} else {
binding.field.set(obj, iter.read(binding.valueTypeLiteral));
}
}
applySetters(temp, obj);
return obj;
}
private Object createNewObject(Object... args) throws Exception {
if (ctor == null) {
return staticFactory.invoke(null, args);
} else {
return ctor.newInstance(args);
}
}
}