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
166 lines (154 loc) · 6.31 KB
/
Codegen.java
File metadata and controls
166 lines (154 loc) · 6.31 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
package com.jsoniter;
import com.jsoniter.spi.*;
import javassist.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.*;
import java.util.*;
class Codegen {
static boolean staticGen = false;
static boolean strictMode = false;
static ClassPool pool = ClassPool.getDefault();
public static void enableStrictMode() {
strictMode = true;
}
static Decoder getDecoder(String cacheKey, Type type) {
Decoder decoder = ExtensionManager.getDecoder(cacheKey);
if (decoder != null) {
return decoder;
}
return gen(cacheKey, type);
}
private synchronized static Decoder gen(String cacheKey, Type type) {
Decoder decoder = ExtensionManager.getDecoder(cacheKey);
if (decoder != null) {
return decoder;
}
for (Extension extension : ExtensionManager.getExtensions()) {
decoder = extension.createDecoder(cacheKey, type);
if (decoder != null) {
ExtensionManager.addNewDecoder(cacheKey, decoder);
return decoder;
}
}
Type[] typeArgs = new Type[0];
Class clazz;
if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
clazz = (Class) pType.getRawType();
typeArgs = pType.getActualTypeArguments();
} else {
clazz = (Class) type;
}
String source = genSource(cacheKey, clazz, typeArgs);
source = "public static 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 {
if (staticGen) {
staticGen(cacheKey, source);
}
decoder = dynamicGen(cacheKey, source);
ExtensionManager.addNewDecoder(cacheKey, decoder);
return decoder;
} catch (Exception e) {
System.err.println("failed to generate decoder for: " + type + " with " + Arrays.toString(typeArgs));
System.err.println(source);
throw new JsonException(e);
}
}
private static void staticGen(String cacheKey, String source) throws IOException {
createDir(cacheKey);
String fileName = cacheKey.replace('.', '/') + ".java";
FileOutputStream fileOutputStream = new FileOutputStream(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 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(".");
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 Decoder dynamicGen(String cacheKey, String source) throws Exception {
Decoder decoder;
CtClass ctClass = pool.makeClass(cacheKey);
ctClass.setInterfaces(new CtClass[]{pool.get(Decoder.class.getName())});
CtMethod staticMethod = CtNewMethod.make(source, ctClass);
ctClass.addMethod(staticMethod);
CtMethod interfaceMethod = CtNewMethod.make("" +
"public Object decode(com.jsoniter.JsonIterator iter) {" +
"return decode_(iter);" +
"}", ctClass);
ctClass.addMethod(interfaceMethod);
decoder = (Decoder) ctClass.toClass().newInstance();
return decoder;
}
private static String genSource(String cacheKey, Class clazz, Type[] typeArgs) {
if (CodegenImplNative.NATIVE_READS.containsKey(clazz.getName())) {
return CodegenImplNative.genNative(clazz.getName());
}
if (clazz.isArray()) {
return CodegenImplArray.genArray(clazz);
}
if (Map.class.isAssignableFrom(clazz)) {
return CodegenImplMap.genMap(clazz, typeArgs);
}
if (Collection.class.isAssignableFrom(clazz)) {
return CodegenImplArray.genCollection(clazz, typeArgs);
}
ClassDescriptor desc = ExtensionManager.getClassDescriptor(clazz, false);
List<Binding> allBindings = desc.allDecoderBindings();
for (Binding binding : allBindings) {
if (binding.failOnMissing || binding.failOnPresent || binding.skip) {
// only slice support mandatory tracking
return CodegenImplObject.genObjectUsingSlice(clazz, cacheKey, desc);
}
}
if (desc.failOnUnknownFields) {
// only slice support unknown field tracking
return CodegenImplObject.genObjectUsingSlice(clazz, cacheKey, desc);
}
if (allBindings.isEmpty()) {
return CodegenImplObject.genObjectUsingSkip(clazz, desc.ctor);
}
if (strictMode) {
return CodegenImplObject.genObjectUsingSlice(clazz, cacheKey, desc);
}
return CodegenImplObject.genObjectUsingHash(clazz, cacheKey, desc);
}
public static void staticGenDecoders(TypeLiteral[] typeLiterals) {
staticGen = true;
for (TypeLiteral typeLiteral : typeLiterals) {
gen(typeLiteral.getDecoderCacheKey(), typeLiteral.getType());
}
}
}