forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegen.java
More file actions
284 lines (269 loc) · 11.3 KB
/
Codegen.java
File metadata and controls
284 lines (269 loc) · 11.3 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
package com.jsoniter;
import com.jsoniter.spi.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.*;
class Codegen {
// only read/write when generating code with synchronized protection
private final static Set<String> generatedClassNames = new HashSet<String>();
static CodegenAccess.StaticCodegenTarget isDoingStaticCodegen = null;
static Decoder getDecoder(String cacheKey, Type type) {
Decoder decoder = JsoniterSpi.getDecoder(cacheKey);
if (decoder != null) {
return decoder;
}
return gen(cacheKey, type);
}
private synchronized static Decoder gen(String cacheKey, Type type) {
Decoder decoder = JsoniterSpi.getDecoder(cacheKey);
if (decoder != null) {
return decoder;
}
List<Extension> extensions = JsoniterSpi.getExtensions();
for (Extension extension : extensions) {
type = extension.chooseImplementation(type);
}
type = chooseImpl(type);
for (Extension extension : extensions) {
decoder = extension.createDecoder(cacheKey, type);
if (decoder != null) {
JsoniterSpi.addNewDecoder(cacheKey, decoder);
return decoder;
}
}
ClassInfo classInfo = new ClassInfo(type);
decoder = CodegenImplNative.NATIVE_DECODERS.get(classInfo.clazz);
if (decoder != null) {
return decoder;
}
addPlaceholderDecoderToSupportRecursiveStructure(cacheKey);
try {
Config currentConfig = JsoniterSpi.getCurrentConfig();
DecodingMode mode = currentConfig.decodingMode();
if (mode == DecodingMode.REFLECTION_MODE) {
decoder = ReflectionDecoderFactory.create(classInfo);
return decoder;
}
if (isDoingStaticCodegen == null) {
try {
decoder = (Decoder) Class.forName(cacheKey).newInstance();
return decoder;
} catch (Exception e) {
if (mode == DecodingMode.STATIC_MODE) {
throw new JsonException("static gen should provide the decoder we need, but failed to create the decoder", e);
}
}
}
String source = genSource(mode, classInfo);
source = "public static java.lang.Object decode_(com.jsoniter.JsonIterator iter) throws java.io.IOException { "
+ source + "}";
if ("true".equals(System.getenv("JSONITER_DEBUG"))) {
System.out.println(">>> " + cacheKey);
System.out.println(source);
}
try {
generatedClassNames.add(cacheKey);
if (isDoingStaticCodegen == null) {
decoder = DynamicCodegen.gen(cacheKey, source);
} else {
staticGen(cacheKey, source);
}
return decoder;
} catch (Exception e) {
String msg = "failed to generate decoder for: " + classInfo + " with " + Arrays.toString(classInfo.typeArgs) + ", exception: " + e;
msg = msg + "\n" + source;
throw new JsonException(msg, e);
}
} finally {
JsoniterSpi.addNewDecoder(cacheKey, decoder);
}
}
private static void addPlaceholderDecoderToSupportRecursiveStructure(final String cacheKey) {
JsoniterSpi.addNewDecoder(cacheKey, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
Decoder decoder = JsoniterSpi.getDecoder(cacheKey);
if (this == decoder) {
for(int i = 0; i < 30; i++) {
decoder = JsoniterSpi.getDecoder(cacheKey);
if (this == decoder) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new JsonException(e);
}
} else {
break;
}
}
if (this == decoder) {
throw new JsonException("internal error: placeholder is not replaced with real decoder");
}
}
return decoder.decode(iter);
}
});
}
public static boolean canStaticAccess(String cacheKey) {
return generatedClassNames.contains(cacheKey);
}
private static Type chooseImpl(Type type) {
Type[] typeArgs = new Type[0];
Class clazz;
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
clazz = (Class) pType.getRawType();
typeArgs = pType.getActualTypeArguments();
} else if (type instanceof WildcardType) {
return Object.class;
} else {
clazz = (Class) type;
}
Class implClazz = JsoniterSpi.getTypeImplementation(clazz);
if (Collection.class.isAssignableFrom(clazz)) {
Type compType = Object.class;
if (typeArgs.length == 0) {
// default to List<Object>
} else if (typeArgs.length == 1) {
compType = typeArgs[0];
} else {
throw new IllegalArgumentException(
"can not bind to generic collection without argument types, " +
"try syntax like TypeLiteral<List<Integer>>{}");
}
if (clazz == List.class) {
clazz = implClazz == null ? ArrayList.class : implClazz;
} else if (clazz == Set.class) {
clazz = implClazz == null ? HashSet.class : implClazz;
}
return GenericsHelper.createParameterizedType(new Type[]{compType}, null, clazz);
}
if (Map.class.isAssignableFrom(clazz)) {
Type keyType = String.class;
Type valueType = Object.class;
if (typeArgs.length == 0) {
// default to Map<String, Object>
} else if (typeArgs.length == 2) {
keyType = typeArgs[0];
valueType = typeArgs[1];
} else {
throw new IllegalArgumentException(
"can not bind to generic collection without argument types, " +
"try syntax like TypeLiteral<Map<String, String>>{}");
}
if (clazz == Map.class) {
clazz = implClazz == null ? HashMap.class : implClazz;
}
if (keyType == Object.class) {
keyType = String.class;
}
MapKeyDecoders.registerOrGetExisting(keyType);
return GenericsHelper.createParameterizedType(new Type[]{keyType, valueType}, null, clazz);
}
if (implClazz != null) {
if (typeArgs.length == 0) {
return implClazz;
} else {
return GenericsHelper.createParameterizedType(typeArgs, null, implClazz);
}
}
return type;
}
private static void staticGen(String cacheKey, String source) throws IOException {
createDir(cacheKey);
String fileName = cacheKey.replace('.', '/') + ".java";
FileOutputStream fileOutputStream = new FileOutputStream(new File(isDoingStaticCodegen.outputDir, fileName));
try {
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream);
try {
staticGen(cacheKey, writer, source);
} finally {
writer.close();
}
} finally {
fileOutputStream.close();
}
}
private static void staticGen(String cacheKey, OutputStreamWriter writer, String source) throws IOException {
String className = cacheKey.substring(cacheKey.lastIndexOf('.') + 1);
String packageName = cacheKey.substring(0, cacheKey.lastIndexOf('.'));
writer.write("package " + packageName + ";\n");
writer.write("public class " + className + " implements com.jsoniter.spi.Decoder {\n");
writer.write(source);
writer.write("public java.lang.Object decode(com.jsoniter.JsonIterator iter) throws java.io.IOException {\n");
writer.write("return decode_(iter);\n");
writer.write("}\n");
writer.write("}\n");
}
private static void createDir(String cacheKey) {
String[] parts = cacheKey.split("\\.");
File parent = new File(isDoingStaticCodegen.outputDir);
for (int i = 0; i < parts.length - 1; i++) {
String part = parts[i];
File current = new File(parent, part);
current.mkdir();
parent = current;
}
}
private static String genSource(DecodingMode mode, ClassInfo classInfo) {
if (classInfo.clazz.isArray()) {
return CodegenImplArray.genArray(classInfo);
}
if (Map.class.isAssignableFrom(classInfo.clazz)) {
return CodegenImplMap.genMap(classInfo);
}
if (Collection.class.isAssignableFrom(classInfo.clazz)) {
return CodegenImplArray.genCollection(classInfo);
}
if (classInfo.clazz.isEnum()) {
return CodegenImplEnum.genEnum(classInfo);
}
ClassDescriptor desc = ClassDescriptor.getDecodingClassDescriptor(classInfo, false);
if (shouldUseStrictMode(mode, desc)) {
return CodegenImplObjectStrict.genObjectUsingStrict(desc);
} else {
return CodegenImplObjectHash.genObjectUsingHash(desc);
}
}
private static boolean shouldUseStrictMode(DecodingMode mode, ClassDescriptor desc) {
if (mode == DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_STRICTLY) {
return true;
}
List<Binding> allBindings = desc.allDecoderBindings();
for (Binding binding : allBindings) {
if (binding.asMissingWhenNotPresent || binding.asExtraWhenPresent || binding.shouldSkip) {
// only slice support mandatory tracking
return true;
}
}
if (desc.asExtraForUnknownProperties) {
// only slice support unknown field tracking
return true;
}
if (!desc.keyValueTypeWrappers.isEmpty()) {
return true;
}
boolean hasBinding = false;
for (Binding allBinding : allBindings) {
if (allBinding.fromNames.length > 0) {
hasBinding = true;
}
}
if (!hasBinding) {
// empty object can only be handled by strict mode
return true;
}
return false;
}
public static void staticGenDecoders(TypeLiteral[] typeLiterals, CodegenAccess.StaticCodegenTarget staticCodegenTarget) {
isDoingStaticCodegen = staticCodegenTarget;
for (TypeLiteral typeLiteral : typeLiterals) {
gen(typeLiteral.getDecoderCacheKey(), typeLiteral.getType());
}
}
}