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
138 lines (127 loc) · 5.37 KB
/
Codegen.java
File metadata and controls
138 lines (127 loc) · 5.37 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
package com.jsoniter;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import java.lang.reflect.*;
import java.util.*;
class Codegen {
static boolean strictMode = false;
static volatile Map<String, Decoder> cache = new HashMap<String, Decoder>();
static ClassPool pool = ClassPool.getDefault();
public static void enableStrictMode() {
strictMode = true;
}
static Decoder getDecoder(String cacheKey, Type type, Type... typeArgs) {
Decoder decoder = cache.get(cacheKey);
if (decoder != null) {
return decoder;
}
return gen(cacheKey, type, typeArgs);
}
private synchronized static Decoder gen(String cacheKey, Type type, Type[] typeArgs) {
Decoder decoder = cache.get(cacheKey);
if (decoder != null) {
return decoder;
}
for (Extension extension : ExtensionManager.extensions) {
decoder = extension.createDecoder(type, typeArgs);
if (decoder != null) {
addNewDecoder(cacheKey, decoder);
return decoder;
}
}
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);
if ("true".equals(System.getenv("JSONITER_DEBUG"))) {
System.out.println(">>> " + cacheKey);
System.out.println(source);
}
try {
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();
addNewDecoder(cacheKey, decoder);
return decoder;
} catch (Exception e) {
System.err.println("failed to generate encoder for: " + type + " with " + Arrays.toString(typeArgs));
System.err.println(source);
throw new RuntimeException(e);
}
}
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 genMap(clazz, typeArgs);
}
if (Collection.class.isAssignableFrom(clazz)) {
return CodegenImplArray.genCollection(clazz, typeArgs);
}
CustomizedConstructor ctor = ExtensionManager.getCtor(clazz);
List<CustomizedSetter> setters = ExtensionManager.getSetters(clazz);
List<Binding> fields = ExtensionManager.getFields(clazz);
if (strictMode) {
return CodegenImplObject.genObjectUsingSlice(clazz, cacheKey, ctor, setters, fields);
} else {
return CodegenImplObject.genObjectUsingHash(clazz, cacheKey, ctor, setters, fields);
}
}
private static String genMap(Class clazz, Type[] typeArgs) {
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 (keyType != String.class) {
throw new IllegalArgumentException("map key must be String");
}
if (clazz == Map.class) {
clazz = HashMap.class;
}
StringBuilder lines = new StringBuilder();
append(lines, "public static Object decode_(com.jsoniter.JsonIterator iter) {");
append(lines, "{{clazz}} map = ({{clazz}})com.jsoniter.CodegenAccess.resetExistingObject(iter);");
append(lines, "if (map == null) { map = new {{clazz}}(); }");
append(lines, "for (String field = iter.readObject(); field != null; field = iter.readObject()) {");
append(lines, "map.put(field, {{op}});");
append(lines, "}");
append(lines, "return map;");
append(lines, "}");
return lines.toString().replace("{{clazz}}", clazz.getName()).replace("{{op}}", CodegenImplNative.genReadOp(valueType));
}
public static void addNewDecoder(String cacheKey, Decoder decoder) {
HashMap<String, Decoder> newCache = new HashMap<String, Decoder>(cache);
newCache.put(cacheKey, decoder);
cache = newCache;
}
private static void append(StringBuilder lines, String str) {
lines.append(str);
lines.append("\n");
}
}