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
31 lines (26 loc) · 1.1 KB
/
DynamicCodegen.java
File metadata and controls
31 lines (26 loc) · 1.1 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
package com.jsoniter.output;
import com.jsoniter.spi.Encoder;
import javassist.*;
class DynamicCodegen {
static ClassPool pool = ClassPool.getDefault();
static {
pool.insertClassPath(new ClassClassPath(Encoder.class));
}
public static Encoder gen(Class clazz, String cacheKey, CodegenResult source) throws Exception {
source.flushBuffer();
CtClass ctClass = pool.makeClass(cacheKey);
ctClass.setInterfaces(new CtClass[]{pool.get(Encoder.class.getName())});
String staticCode = source.toString();
CtMethod staticMethod = CtNewMethod.make(staticCode, ctClass);
ctClass.addMethod(staticMethod);
String wrapperCode = source.generateWrapperCode(clazz);
if ("true".equals(System.getenv("JSONITER_DEBUG"))) {
System.out.println(">>> " + cacheKey);
System.out.println(wrapperCode);
System.out.println(staticCode);
}
CtMethod interfaceMethod = CtNewMethod.make(wrapperCode, ctClass);
ctClass.addMethod(interfaceMethod);
return (Encoder) ctClass.toClass().newInstance();
}
}