@@ -43,14 +43,13 @@ public class JavaCompilerCheckFacade {
4343 * and
4444 * reports any issues to the provided <code>listener</code>
4545 *
46- * @param listener the {@link ProblemInitializer.ProblemInitializerListener} to be informed
47- * about any
48- * issues found in the target Java program
46+ * @param listener the {@link ProblemInitializer.ProblemInitializerListener} to be informed
47+ * about any issues found in the target Java program
4948 * @param bootClassPath the {@link File} referring to the path containing the core Java classes
50- * @param classPath the {@link List} of {@link File}s referring to the directory that make up
51- * the target Java programs classpath
52- * @param javaPath the {@link String} with the path to the source of the target Java program
53- * @return
49+ * @param classPath the {@link List} of {@link File}s referring to the directory that make up
50+ * the target Java programs classpath
51+ * @param javaPath the {@link String} with the path to the source of the target Java program
52+ * @return future providing the list of diagnostics
5453 */
5554 @ Nonnull
5655 public static CompletableFuture <List <PositionedIssueString >> check (
@@ -71,60 +70,77 @@ public static CompletableFuture<List<PositionedIssueString>> check(
7170 return CompletableFuture .completedFuture (Collections .emptyList ());
7271 }
7372
74- var fileManager =
75- new JavaFileManagerDelegate (
76- compiler .getStandardFileManager (
77- diagnostics , Locale .ENGLISH , Charset .defaultCharset ()));
73+ JavaFileManagerDelegate fileManager =
74+ new JavaFileManagerDelegate (
75+ compiler .getStandardFileManager (
76+ diagnostics , Locale .ENGLISH , Charset .defaultCharset ()));
7877
79- var output = new StringWriter ();
80- var classes = new ArrayList <String >();
78+ StringWriter output = new StringWriter ();
79+ List < String > classes = new ArrayList <>();
8180
82- var paths = new ArrayList <File >();
81+ // gather configured bootstrap classpath and regular classpath
82+ List <File > paths = new ArrayList <>();
8383 if (bootClassPath != null ) {
8484 paths .add (bootClassPath );
8585 }
8686 if (classPath != null && !classPath .isEmpty ()) {
8787 paths .addAll (classPath );
8888 }
8989 paths .add (javaPath );
90- var compilationUnits =
91- fileManager .getJavaFileObjects (paths .stream ()
92- .filter (File ::isDirectory )
93- .flatMap (it -> {
94- try {
95- return Files .walk (it .toPath ())
96- .filter (f -> !Files .isDirectory (f ))
97- .filter (f -> f .getFileName ().toString ().endsWith (".java" ));
98- } catch (IOException e ) {
99- LOGGER .info ("" , e );
100- return Stream .empty ();
101- }
102- }).toArray (Path []::new ));
103-
104- var task = compiler .getTask (output , fileManager , diagnostics ,
105- new ArrayList <>(), classes , compilationUnits );
90+ Iterable <? extends JavaFileObject > compilationUnits =
91+ fileManager .getJavaFileObjects (paths .stream ()
92+ .filter (File ::isDirectory )
93+ .flatMap (it -> {
94+ try {
95+ return Files .walk (it .toPath ())
96+ .filter (f -> !Files .isDirectory (f ))
97+ .filter (f -> f .getFileName ().toString ().endsWith (".java" ));
98+ } catch (IOException e ) {
99+ LOGGER .info ("" , e );
100+ return Stream .empty ();
101+ }
102+ }).toArray (Path []::new ));
103+
104+ JavaCompiler . CompilationTask task = compiler .getTask (output , fileManager , diagnostics ,
105+ new ArrayList <>(), classes , compilationUnits );
106106
107107 return CompletableFuture .supplyAsync (() -> {
108108 long start = System .currentTimeMillis ();
109109 var b = task .call ();
110110 LOGGER .info ("Javac check took {} ms." , System .currentTimeMillis () - start );
111- for (var diagnostic : diagnostics .getDiagnostics ()) {
111+ for (Diagnostic <? extends JavaFileObject > diagnostic : diagnostics .getDiagnostics ()) {
112112 LOGGER .info ("{}" , diagnostic );
113113 }
114114 return diagnostics .getDiagnostics ().stream ().map (
115- it -> new PositionedIssueString (
116- it .getMessage (Locale .ENGLISH ),
117- fileManager .asPath (it .getSource ()).toFile ().getAbsolutePath (),
118- new Position ((int ) it .getLineNumber (), (int ) it .getColumnNumber ()),
119- "" + it .getCode () + " " + it .getKind ()))
115+ it -> new PositionedIssueString (
116+ it .getMessage (Locale .ENGLISH ),
117+ fileManager .asPath (it .getSource ()).toFile ().getAbsolutePath (),
118+ new Position ((int ) it .getLineNumber (), (int ) it .getColumnNumber ()),
119+ "" + it .getCode () + " " + it .getKind ()))
120120 .collect (Collectors .toList ());
121121 });
122122 }
123123}
124124
125+
126+ /**
127+ * Wrapper around a {@link StandardJavaFileManager} that returns a dummy output for
128+ * class files ({@link #getJavaFileForOutput(Location, String, JavaFileObject.Kind, FileObject)}.
129+ * Every other request is delegated to the provided file manager.
130+ *
131+ * @author Alexander Weigl
132+ */
125133class JavaFileManagerDelegate implements StandardJavaFileManager {
134+ /**
135+ * The file manager most calls are delegated to.
136+ */
126137 private final StandardJavaFileManager fileManager ;
127138
139+ /**
140+ * Construct a new wrapper.
141+ *
142+ * @param jfm file manager
143+ */
128144 public JavaFileManagerDelegate (StandardJavaFileManager jfm ) {
129145 this .fileManager = jfm ;
130146 }
@@ -135,14 +151,16 @@ public boolean isSameFile(FileObject a, FileObject b) {
135151 }
136152
137153 @ Override
138- public Iterable <? extends JavaFileObject > getJavaFileObjectsFromFiles (Iterable <? extends File > files ) {
154+ public Iterable <? extends JavaFileObject > getJavaFileObjectsFromFiles (
155+ Iterable <? extends File > files ) {
139156 return fileManager .getJavaFileObjectsFromFiles (files );
140157 }
141158
142159
143160 @ Override
144161 @ Deprecated (since = "13" )
145- public Iterable <? extends JavaFileObject > getJavaFileObjectsFromPaths (Iterable <? extends Path > paths ) {
162+ public Iterable <? extends JavaFileObject > getJavaFileObjectsFromPaths (
163+ Iterable <? extends Path > paths ) {
146164 return fileManager .getJavaFileObjectsFromPaths (paths );
147165 }
148166
@@ -157,7 +175,8 @@ public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) {
157175 }
158176
159177 @ Override
160- public Iterable <? extends JavaFileObject > getJavaFileObjectsFromStrings (Iterable <String > names ) {
178+ public Iterable <? extends JavaFileObject > getJavaFileObjectsFromStrings (
179+ Iterable <String > names ) {
161180 return fileManager .getJavaFileObjectsFromStrings (names );
162181 }
163182
@@ -172,12 +191,14 @@ public void setLocation(Location location, Iterable<? extends File> files) throw
172191 }
173192
174193 @ Override
175- public void setLocationFromPaths (Location location , Collection <? extends Path > paths ) throws IOException {
194+ public void setLocationFromPaths (Location location , Collection <? extends Path > paths )
195+ throws IOException {
176196 fileManager .setLocationFromPaths (location , paths );
177197 }
178198
179199 @ Override
180- public void setLocationForModule (Location location , String moduleName , Collection <? extends Path > paths ) throws IOException {
200+ public void setLocationForModule (Location location , String moduleName ,
201+ Collection <? extends Path > paths ) throws IOException {
181202 fileManager .setLocationForModule (location , moduleName , paths );
182203 }
183204
@@ -207,7 +228,8 @@ public ClassLoader getClassLoader(Location location) {
207228 }
208229
209230 @ Override
210- public Iterable <JavaFileObject > list (Location location , String packageName , Set <JavaFileObject .Kind > kinds , boolean recurse ) throws IOException {
231+ public Iterable <JavaFileObject > list (Location location , String packageName ,
232+ Set <JavaFileObject .Kind > kinds , boolean recurse ) throws IOException {
211233 return fileManager .list (location , packageName , kinds , recurse );
212234 }
213235
@@ -227,13 +249,16 @@ public boolean hasLocation(Location location) {
227249 }
228250
229251 @ Override
230- public JavaFileObject getJavaFileForInput (Location location , String className , JavaFileObject .Kind kind ) throws IOException {
252+ public JavaFileObject getJavaFileForInput (Location location , String className ,
253+ JavaFileObject .Kind kind ) throws IOException {
231254 return fileManager .getJavaFileForInput (location , className , kind );
232255 }
233256
234257 @ Override
235- public JavaFileObject getJavaFileForOutput (Location location , String className , JavaFileObject .Kind kind , FileObject sibling ) throws IOException {
258+ public JavaFileObject getJavaFileForOutput (Location location , String className ,
259+ JavaFileObject .Kind kind , FileObject sibling ) throws IOException {
236260 if (kind == JavaFileObject .Kind .CLASS && location == StandardLocation .CLASS_OUTPUT ) {
261+ // do not save compiled .class files on disk
237262 try {
238263 return new IgnoreOutputJavaFileObject (className , kind );
239264 } catch (URISyntaxException e ) {
@@ -245,12 +270,14 @@ public JavaFileObject getJavaFileForOutput(Location location, String className,
245270
246271
247272 @ Override
248- public FileObject getFileForInput (Location location , String packageName , String relativeName ) throws IOException {
273+ public FileObject getFileForInput (Location location , String packageName , String relativeName )
274+ throws IOException {
249275 return fileManager .getFileForInput (location , packageName , relativeName );
250276 }
251277
252278 @ Override
253- public FileObject getFileForOutput (Location location , String packageName , String relativeName , FileObject sibling ) throws IOException {
279+ public FileObject getFileForOutput (Location location , String packageName , String relativeName ,
280+ FileObject sibling ) throws IOException {
254281 return fileManager .getFileForOutput (location , packageName , relativeName , sibling );
255282 }
256283
@@ -276,7 +303,8 @@ public Location getLocationForModule(Location location, JavaFileObject fo) throw
276303 }
277304
278305 @ Override
279- public <S > ServiceLoader <S > getServiceLoader (Location location , Class <S > service ) throws IOException {
306+ public <S > ServiceLoader <S > getServiceLoader (Location location , Class <S > service )
307+ throws IOException {
280308 return fileManager .getServiceLoader (location , service );
281309 }
282310
@@ -302,6 +330,11 @@ public int isSupportedOption(String option) {
302330}
303331
304332
333+ /**
334+ * Java file object that ignores all writes.
335+ *
336+ * @author Alexander Weigl
337+ */
305338class IgnoreOutputJavaFileObject extends SimpleJavaFileObject {
306339 public IgnoreOutputJavaFileObject (final String name , Kind kind ) throws URISyntaxException {
307340 super (new URI ("memory://" + name + ".class" ), kind );
@@ -310,10 +343,6 @@ public IgnoreOutputJavaFileObject(final String name, Kind kind) throws URISyntax
310343 // ignore written class output
311344 @ Override
312345 public OutputStream openOutputStream () {
313- return new OutputStream () {
314- @ Override
315- public void write (int b ) {
316- }
317- };
346+ return OutputStream .nullOutputStream ();
318347 }
319348}
0 commit comments