forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsoniterSpi.java
More file actions
332 lines (300 loc) · 12.6 KB
/
JsoniterSpi.java
File metadata and controls
332 lines (300 loc) · 12.6 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.jsoniter.spi;
import com.jsoniter.JsonException;
import java.lang.reflect.*;
import java.util.*;
public class JsoniterSpi {
static List<Extension> extensions = new ArrayList<Extension>();
static Map<Class, Class> typeImpls = new HashMap<Class, Class>();
static volatile Map<String, Encoder> encoders = new HashMap<String, Encoder>();
static volatile Map<String, Decoder> decoders = new HashMap<String, Decoder>();
public static void registerExtension(Extension extension) {
extensions.add(extension);
}
public static List<Extension> getExtensions() {
return Collections.unmodifiableList(extensions);
}
public static void registerTypeImplementation(Class superClazz, Class implClazz) {
typeImpls.put(superClazz, implClazz);
}
public static Class getTypeImplementation(Class superClazz) {
return typeImpls.get(superClazz);
}
public static void registerTypeDecoder(Class clazz, Decoder decoder) {
addNewDecoder(TypeLiteral.create(clazz).getDecoderCacheKey(), decoder);
}
public static void registerTypeDecoder(TypeLiteral typeLiteral, Decoder decoder) {
addNewDecoder(typeLiteral.getDecoderCacheKey(), decoder);
}
public static void registerPropertyDecoder(Class clazz, String field, Decoder decoder) {
addNewDecoder(field + "@" + TypeLiteral.create(clazz).getDecoderCacheKey(), decoder);
}
public static void registerPropertyDecoder(TypeLiteral typeLiteral, String field, Decoder decoder) {
addNewDecoder(field + "@" + typeLiteral.getDecoderCacheKey(), decoder);
}
public static void registerTypeEncoder(Class clazz, Encoder encoder) {
addNewEncoder(TypeLiteral.create(clazz).getEncoderCacheKey(), encoder);
}
public static void registerTypeEncoder(TypeLiteral typeLiteral, Encoder encoder) {
addNewEncoder(typeLiteral.getDecoderCacheKey(), encoder);
}
public static void registerPropertyEncoder(Class clazz, String field, Encoder encoder) {
addNewEncoder(field + "@" + TypeLiteral.create(clazz).getEncoderCacheKey(), encoder);
}
public static void registerPropertyEncoder(TypeLiteral typeLiteral, String field, Encoder encoder) {
addNewEncoder(field + "@" + typeLiteral.getDecoderCacheKey(), encoder);
}
public static Decoder getDecoder(String cacheKey) {
return decoders.get(cacheKey);
}
public synchronized static void addNewDecoder(String cacheKey, Decoder decoder) {
HashMap<String, Decoder> newCache = new HashMap<String, Decoder>(decoders);
newCache.put(cacheKey, decoder);
decoders = newCache;
}
public static Encoder getEncoder(String cacheKey) {
return encoders.get(cacheKey);
}
public synchronized static void addNewEncoder(String cacheKey, Encoder encoder) {
HashMap<String, Encoder> newCache = new HashMap<String, Encoder>(encoders);
newCache.put(cacheKey, encoder);
encoders = newCache;
}
public static ClassDescriptor getClassDescriptor(Class clazz, boolean includingPrivate) {
Map<String, Type> lookup = collectTypeVariableLookup(clazz);
ClassDescriptor desc = new ClassDescriptor();
desc.clazz = clazz;
desc.lookup = lookup;
desc.ctor = getCtor(clazz);
desc.fields = getFields(lookup, clazz, includingPrivate);
desc.fields.addAll(getSetters(lookup, clazz, includingPrivate));
desc.wrappers = new ArrayList<WrapperDescriptor>();
desc.getters = getGetters(lookup, clazz, includingPrivate);
for (Extension extension : extensions) {
extension.updateClassDescriptor(desc);
}
setterOrCtorOverrideField(desc);
if (includingPrivate) {
if (desc.ctor.ctor != null) {
desc.ctor.ctor.setAccessible(true);
}
if (desc.ctor.staticFactory != null) {
desc.ctor.staticFactory.setAccessible(true);
}
for (WrapperDescriptor setter : desc.wrappers) {
setter.method.setAccessible(true);
}
}
for (Binding binding : desc.allDecoderBindings()) {
if (binding.fromNames == null) {
binding.fromNames = new String[]{binding.name};
}
if (binding.field != null && includingPrivate) {
binding.field.setAccessible(true);
}
if (binding.method != null && includingPrivate) {
binding.method.setAccessible(true);
}
if (binding.decoder != null) {
JsoniterSpi.addNewDecoder(binding.decoderCacheKey(), binding.decoder);
}
}
for (Binding binding : desc.allEncoderBindings()) {
if (binding.toNames == null) {
binding.toNames = new String[]{binding.name};
}
if (binding.field != null && includingPrivate) {
binding.field.setAccessible(true);
}
if (binding.method != null && includingPrivate) {
binding.method.setAccessible(true);
}
if (binding.encoder != null) {
JsoniterSpi.addNewEncoder(binding.encoderCacheKey(), binding.encoder);
}
}
return desc;
}
private static void setterOrCtorOverrideField(ClassDescriptor desc) {
HashMap<String, Binding> fields = new HashMap<String, Binding>();
for (Binding field : new ArrayList<Binding>(desc.fields)) {
if (fields.containsKey(field.name)) {
// conflict
if (field.method != null) {
// this is method, prefer using it
desc.fields.remove(fields.get(field.name));
fields.put(field.name, field);
} else {
// this is not method, discard it
desc.fields.remove(field);
}
} else {
fields.put(field.name, field);
}
}
for (WrapperDescriptor setter : desc.wrappers) {
for (Binding parameter : setter.parameters) {
if (fields.containsKey(parameter.name)) {
desc.fields.remove(fields.get(parameter.name));
}
}
}
for (Binding parameter : desc.ctor.parameters) {
if (fields.containsKey(parameter.name)) {
desc.fields.remove(fields.get(parameter.name));
}
}
}
private static ConstructorDescriptor getCtor(Class clazz) {
ConstructorDescriptor cctor = new ConstructorDescriptor();
try {
cctor.ctor = clazz.getDeclaredConstructor();
} catch (Exception e) {
cctor.ctor = null;
}
return cctor;
}
private static List<Binding> getFields(Map<String, Type> lookup, Class clazz, boolean includingPrivate) {
ArrayList<Binding> bindings = new ArrayList<Binding>();
for (Field field : getAllFields(clazz, includingPrivate)) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
if (includingPrivate) {
field.setAccessible(true);
}
Binding binding = createBindingFromField(lookup, clazz, field);
bindings.add(binding);
}
return bindings;
}
private static Binding createBindingFromField(Map<String, Type> lookup, Class clazz, Field field) {
Binding binding = new Binding(clazz, lookup, field.getGenericType());
binding.fromNames = new String[]{field.getName()};
binding.name = field.getName();
binding.annotations = field.getAnnotations();
binding.field = field;
return binding;
}
private static List<Field> getAllFields(Class clazz, boolean includingPrivate) {
List<Field> allFields = Arrays.asList(clazz.getFields());
if (includingPrivate) {
allFields = new ArrayList<Field>();
Class current = clazz;
while (current != null) {
allFields.addAll(Arrays.asList(current.getDeclaredFields()));
current = current.getSuperclass();
}
}
return allFields;
}
private static List<Binding> getSetters(Map<String, Type> lookup, Class clazz, boolean includingPrivate) {
ArrayList<Binding> setters = new ArrayList<Binding>();
List<Method> allMethods = Arrays.asList(clazz.getMethods());
if (includingPrivate) {
allMethods = new ArrayList<Method>();
Class current = clazz;
while (current != null) {
allMethods.addAll(Arrays.asList(current.getDeclaredMethods()));
current = current.getSuperclass();
}
}
for (Method method : allMethods) {
if (Modifier.isStatic(method.getModifiers())) {
continue;
}
String methodName = method.getName();
if (methodName.length() < 4) {
continue;
}
if (!methodName.startsWith("set")) {
continue;
}
Type[] paramTypes = method.getGenericParameterTypes();
if (paramTypes.length != 1) {
continue;
}
if (includingPrivate) {
method.setAccessible(true);
}
String fromName = translateSetterName(methodName);
Binding binding = new Binding(clazz, lookup, paramTypes[0]);
binding.fromNames = new String[]{fromName};
binding.name = fromName;
binding.method = method;
setters.add(binding);
}
return setters;
}
private static String translateSetterName(String methodName) {
if (!methodName.startsWith("set")) {
return null;
}
String fromName = methodName.substring("set".length());
char[] fromNameChars = fromName.toCharArray();
fromNameChars[0] = Character.toLowerCase(fromNameChars[0]);
fromName = new String(fromNameChars);
return fromName;
}
private static List<Binding> getGetters(Map<String, Type> lookup, Class clazz, boolean includingPrivate) {
ArrayList<Binding> getters = new ArrayList<Binding>();
for (Method method : clazz.getMethods()) {
if (Modifier.isStatic(method.getModifiers())) {
continue;
}
String methodName = method.getName();
if ("getClass".equals(methodName)) {
continue;
}
if (methodName.length() < 4) {
continue;
}
if (!methodName.startsWith("get")) {
continue;
}
if (method.getGenericParameterTypes().length != 0) {
continue;
}
String toName = methodName.substring("get".length());
char[] fromNameChars = toName.toCharArray();
fromNameChars[0] = Character.toLowerCase(fromNameChars[0]);
toName = new String(fromNameChars);
Binding getter = new Binding(clazz, lookup, method.getGenericReturnType());
getter.toNames = new String[]{toName};
getter.name = methodName + "()";
getter.method = method;
getters.add(getter);
}
return getters;
}
public static void dump() {
for (String cacheKey : decoders.keySet()) {
System.err.println(cacheKey);
}
for (String cacheKey : encoders.keySet()) {
System.err.println(cacheKey);
}
}
private static Map<String, Type> collectTypeVariableLookup(Type type) {
HashMap<String, Type> vars = new HashMap<String, Type>();
if (null == type) {
return vars;
}
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
Type[] actualTypeArguments = pType.getActualTypeArguments();
Class clazz = (Class) pType.getRawType();
for (int i = 0; i < clazz.getTypeParameters().length; i++) {
TypeVariable variable = clazz.getTypeParameters()[i];
vars.put(variable.getName() + "@" + clazz.getCanonicalName(), actualTypeArguments[i]);
}
vars.putAll(collectTypeVariableLookup(clazz.getGenericSuperclass()));
return vars;
}
if (type instanceof Class) {
Class clazz = (Class) type;
vars.putAll(collectTypeVariableLookup(clazz.getGenericSuperclass()));
return vars;
}
throw new JsonException("unexpected type: " + type);
}
}