1
1
package com .jsoniter .output ;
2
2
3
3
import com .jsoniter .any .Any ;
4
- import com .jsoniter .spi .JsonException ;
5
- import com .jsoniter .spi .Encoder ;
6
- import com .jsoniter .spi .Extension ;
7
- import com .jsoniter .spi .JsoniterSpi ;
8
- import com .jsoniter .spi .TypeLiteral ;
4
+ import com .jsoniter .spi .*;
9
5
10
6
import java .io .File ;
11
7
import java .io .FileOutputStream ;
19
15
class Codegen {
20
16
21
17
static EncodingMode mode = EncodingMode .REFLECTION_MODE ;
22
- static boolean isDoingStaticCodegen ;
18
+ static StaticCodegenTarget isDoingStaticCodegen ;
23
19
// only read/write when generating code with synchronized protection
24
20
private final static Map <String , CodegenResult > generatedSources = new HashMap <String , CodegenResult >();
25
21
private volatile static Map <String , Encoder > reflectionEncoders = new HashMap <String , Encoder >();
@@ -108,7 +104,7 @@ private static synchronized Encoder gen(final String cacheKey, Type type) {
108
104
JsoniterSpi .addNewEncoder (cacheKey , encoder );
109
105
return encoder ;
110
106
}
111
- if (! isDoingStaticCodegen ) {
107
+ if (isDoingStaticCodegen == null ) {
112
108
try {
113
109
encoder = (Encoder ) Class .forName (cacheKey ).newInstance ();
114
110
JsoniterSpi .addNewEncoder (cacheKey , encoder );
@@ -123,10 +119,10 @@ private static synchronized Encoder gen(final String cacheKey, Type type) {
123
119
CodegenResult source = genSource (cacheKey , clazz , typeArgs );
124
120
try {
125
121
generatedSources .put (cacheKey , source );
126
- if (isDoingStaticCodegen ) {
127
- staticGen (clazz , cacheKey , source );
128
- } else {
122
+ if (isDoingStaticCodegen == null ) {
129
123
encoder = DynamicCodegen .gen (clazz , cacheKey , source );
124
+ } else {
125
+ staticGen (clazz , cacheKey , source );
130
126
}
131
127
JsoniterSpi .addNewEncoder (cacheKey , encoder );
132
128
return encoder ;
@@ -165,7 +161,7 @@ public static CodegenResult getGeneratedSource(String cacheKey) {
165
161
private static void staticGen (Class clazz , String cacheKey , CodegenResult source ) throws IOException {
166
162
createDir (cacheKey );
167
163
String fileName = cacheKey .replace ('.' , '/' ) + ".java" ;
168
- FileOutputStream fileOutputStream = new FileOutputStream (fileName );
164
+ FileOutputStream fileOutputStream = new FileOutputStream (new File ( isDoingStaticCodegen . outputDir , fileName ) );
169
165
try {
170
166
OutputStreamWriter writer = new OutputStreamWriter (fileOutputStream );
171
167
try {
@@ -190,7 +186,7 @@ private static void staticGen(Class clazz, String cacheKey, OutputStreamWriter w
190
186
191
187
private static void createDir (String cacheKey ) {
192
188
String [] parts = cacheKey .split ("\\ ." );
193
- File parent = new File ("." );
189
+ File parent = new File (isDoingStaticCodegen . outputDir );
194
190
for (int i = 0 ; i < parts .length - 1 ; i ++) {
195
191
String part = parts [i ];
196
192
File current = new File (parent , part );
@@ -215,8 +211,8 @@ private static CodegenResult genSource(String cacheKey, Class clazz, Type[] type
215
211
return CodegenImplObject .genObject (clazz );
216
212
}
217
213
218
- public static void staticGenEncoders (TypeLiteral [] typeLiterals ) {
219
- isDoingStaticCodegen = true ;
214
+ public static void staticGenEncoders (TypeLiteral [] typeLiterals , StaticCodegenTarget staticCodegenTarget ) {
215
+ isDoingStaticCodegen = staticCodegenTarget ;
220
216
for (TypeLiteral typeLiteral : typeLiterals ) {
221
217
gen (typeLiteral .getEncoderCacheKey (), typeLiteral .getType ());
222
218
}
0 commit comments