forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicCodegen.java
More file actions
34 lines (28 loc) · 1.15 KB
/
DynamicCodegen.java
File metadata and controls
34 lines (28 loc) · 1.15 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
package com.jsoniter;
import com.jsoniter.spi.Decoder;
import javassist.*;
class DynamicCodegen {
static ClassPool pool = ClassPool.getDefault();
static {
pool.insertClassPath(new ClassClassPath(Decoder.class));
}
public static Decoder gen(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;
}
public static void enableStreamingSupport() throws Exception {
CtClass ctClass = pool.makeClass("com.jsoniter.IterImpl");
ctClass.setSuperclass(pool.get(IterImplForStreaming.class.getName()));
ctClass.toClass();
}
}