33
33
import com .sun .source .tree .MemberSelectTree ;
34
34
import com .sun .source .tree .MethodTree ;
35
35
import com .sun .source .tree .ModifiersTree ;
36
+ import com .sun .source .tree .PrimitiveTypeTree ;
36
37
import com .sun .source .tree .Tree ;
37
38
import com .sun .source .tree .VariableTree ;
38
39
import java .io .File ;
@@ -140,9 +141,12 @@ public String getName() {
140
141
public void init (JavacTask jt , String ... args ) {
141
142
addExports ();
142
143
144
+ Path internalAPIDigestFile ;
145
+ Map <String , String > internalAPI = new HashMap <>();
143
146
AtomicBoolean noApiChange = new AtomicBoolean ();
147
+ Context context = ((BasicJavacTask ) jt ).getContext ();
148
+ JavaCompiler compiler = JavaCompiler .instance (context );
144
149
try {
145
- Context context = ((BasicJavacTask ) jt ).getContext ();
146
150
Options options = Options .instance (context );
147
151
String modifiedInputs = options .get ("modifiedInputs" );
148
152
if (modifiedInputs == null ) {
@@ -157,8 +161,18 @@ public void init(JavacTask jt, String... args) {
157
161
Set <Path > modified = Files .readAllLines (Paths .get (modifiedInputs )).stream ()
158
162
.map (Paths ::get )
159
163
.collect (Collectors .toSet ());
160
- Path internalAPIDigestFile = Paths .get (internalAPIPath );
161
- JavaCompiler compiler = JavaCompiler .instance (context );
164
+ internalAPIDigestFile = Paths .get (internalAPIPath );
165
+ if (Files .isReadable (internalAPIDigestFile )) {
166
+ try {
167
+ Files .readAllLines (internalAPIDigestFile , StandardCharsets .UTF_8 )
168
+ .forEach (line -> {
169
+ String [] keyAndValue = line .split ("=" );
170
+ internalAPI .put (keyAndValue [0 ], keyAndValue [1 ]);
171
+ });
172
+ } catch (IOException ex ) {
173
+ throw new IllegalStateException (ex );
174
+ }
175
+ }
162
176
Class <?> initialFileParserIntf = Class .forName ("com.sun.tools.javac.main.JavaCompiler$InitialFileParserIntf" );
163
177
Class <?> initialFileParser = Class .forName ("com.sun.tools.javac.main.JavaCompiler$InitialFileParser" );
164
178
Field initialParserKeyField = initialFileParser .getDeclaredField ("initialParserKey" );
@@ -169,7 +183,7 @@ public void init(JavacTask jt, String... args) {
169
183
new Class <?>[] {initialFileParserIntf },
170
184
new FilteredInitialFileParser (compiler ,
171
185
modified ,
172
- internalAPIDigestFile ,
186
+ internalAPI ,
173
187
noApiChange ,
174
188
debug ));
175
189
context .<Object >put (key , initialParserInstance );
@@ -213,7 +227,17 @@ public void finished(TaskEvent te) {
213
227
}
214
228
}
215
229
}
216
- if (te .getKind () == Kind .COMPILATION && !noApiChange .get ()) {
230
+ if (te .getKind () == Kind .COMPILATION && !noApiChange .get () &&
231
+ compiler .errorCount () == 0 ) {
232
+ try (OutputStream out = Files .newOutputStream (internalAPIDigestFile )) {
233
+ String hashes = internalAPI .entrySet ()
234
+ .stream ()
235
+ .map (e -> e .getKey () + "=" + e .getValue ())
236
+ .collect (Collectors .joining ("\n " ));
237
+ out .write (hashes .getBytes (StandardCharsets .UTF_8 ));
238
+ } catch (IOException ex ) {
239
+ throw new IllegalStateException (ex );
240
+ }
217
241
String previousSignature = null ;
218
242
File digestFile = new File (args [0 ]);
219
243
try (InputStream in = new FileInputStream (digestFile )) {
@@ -258,20 +282,8 @@ public ClassLoader getClassLoader(JavaFileManager.Location location) {
258
282
259
283
private com .sun .tools .javac .util .List <JCCompilationUnit > doFilteredParse (
260
284
JavaCompiler compiler , Iterable <JavaFileObject > fileObjects , Set <Path > modified ,
261
- Path internalAPIDigestFile , AtomicBoolean noApiChange ,
285
+ Map < String , String > internalAPI , AtomicBoolean noApiChange ,
262
286
boolean debug ) {
263
- Map <String , String > internalAPI = new LinkedHashMap <>();
264
- if (Files .isReadable (internalAPIDigestFile )) {
265
- try {
266
- Files .readAllLines (internalAPIDigestFile , StandardCharsets .UTF_8 )
267
- .forEach (line -> {
268
- String [] keyAndValue = line .split ("=" );
269
- internalAPI .put (keyAndValue [0 ], keyAndValue [1 ]);
270
- });
271
- } catch (IOException ex ) {
272
- throw new IllegalStateException (ex );
273
- }
274
- }
275
287
Map <JavaFileObject , JCCompilationUnit > files2CUT = new IdentityHashMap <>();
276
288
boolean fullRecompile = modified .stream ()
277
289
.map (Path ::toString )
@@ -289,7 +301,6 @@ private com.sun.tools.javac.util.List<JCCompilationUnit> doFilteredParse(
289
301
result .add (parsed );
290
302
}
291
303
}
292
-
293
304
if (fullRecompile ) {
294
305
for (JavaFileObject jfo : fileObjects ) {
295
306
if (!modified .contains (Path .of (jfo .getName ()))) {
@@ -301,15 +312,6 @@ private com.sun.tools.javac.util.List<JCCompilationUnit> doFilteredParse(
301
312
result .add (parsed );
302
313
}
303
314
}
304
- try (OutputStream out = Files .newOutputStream (internalAPIDigestFile )) {
305
- String hashes = internalAPI .entrySet ()
306
- .stream ()
307
- .map (e -> e .getKey () + "=" + e .getValue ())
308
- .collect (Collectors .joining ("\n " ));
309
- out .write (hashes .getBytes (StandardCharsets .UTF_8 ));
310
- } catch (IOException ex ) {
311
- throw new IllegalStateException (ex );
312
- }
313
315
} else {
314
316
noApiChange .set (true );
315
317
}
@@ -835,7 +837,7 @@ private boolean importantMember(Tree m) {
835
837
!isPrivate (((VariableTree ) m ).getModifiers ()) ||
836
838
isRecordComponent ((VariableTree ) m );
837
839
case BLOCK -> false ;
838
- default -> throw new IllegalStateException ( "Unexpected tree kind: " + m . getKind ()) ;
840
+ default -> false ;
839
841
};
840
842
}
841
843
@@ -878,24 +880,30 @@ public Void visitModifiers(ModifiersTree node, Void p) {
878
880
return super .visitModifiers (node , p );
879
881
}
880
882
883
+ @ Override
884
+ public Void visitPrimitiveType (PrimitiveTypeTree node , Void p ) {
885
+ update (node .getPrimitiveTypeKind ().name ());
886
+ return super .visitPrimitiveType (node , p );
887
+ }
888
+
881
889
}
882
890
883
891
private class FilteredInitialFileParser implements InvocationHandler {
884
892
885
893
private final JavaCompiler compiler ;
886
894
private final Set <Path > modified ;
887
- private final Path internalAPIDigestFile ;
895
+ private final Map < String , String > internalAPI ;
888
896
private final AtomicBoolean noApiChange ;
889
897
private final boolean debug ;
890
898
891
899
public FilteredInitialFileParser (JavaCompiler compiler ,
892
900
Set <Path > modified ,
893
- Path internalAPIDigestFile ,
901
+ Map < String , String > internalAPI ,
894
902
AtomicBoolean noApiChange ,
895
903
boolean debug ) {
896
904
this .compiler = compiler ;
897
905
this .modified = modified ;
898
- this .internalAPIDigestFile = internalAPIDigestFile ;
906
+ this .internalAPI = internalAPI ;
899
907
this .noApiChange = noApiChange ;
900
908
this .debug = debug ;
901
909
}
@@ -907,7 +915,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
907
915
case "parse" -> doFilteredParse (compiler ,
908
916
(Iterable <JavaFileObject >) args [0 ],
909
917
modified ,
910
- internalAPIDigestFile ,
918
+ internalAPI ,
911
919
noApiChange ,
912
920
debug );
913
921
default -> throw new UnsupportedOperationException ();
0 commit comments