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
30 lines (25 loc) · 1003 Bytes
/
DynamicCodegen.java
File metadata and controls
30 lines (25 loc) · 1003 Bytes
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
package com.jsoniter;
import com.jsoniter.spi.Decoder;
import com.jsoniter.spi.JsoniterSpi;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
import java.lang.reflect.Type;
class DynamicCodegen {
static ClassPool pool = ClassPool.getDefault();
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;
}
}