|
43 | 43 | import javax.tools.JavaCompiler; |
44 | 44 | import javax.tools.JavaCompiler.CompilationTask; |
45 | 45 | import javax.tools.JavaFileObject; |
| 46 | +import javax.tools.StandardJavaFileManager; |
46 | 47 | import javax.tools.StandardLocation; |
47 | 48 | import org.checkerframework.checker.nullness.qual.Nullable; |
48 | 49 |
|
@@ -180,36 +181,44 @@ public final Compilation compile(JavaFileObject... files) { |
180 | 181 | */ |
181 | 182 | public final Compilation compile(Iterable<? extends JavaFileObject> files) { |
182 | 183 | DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>(); |
183 | | - InMemoryJavaFileManager fileManager = |
184 | | - new InMemoryJavaFileManager( |
185 | | - javaCompiler().getStandardFileManager(diagnosticCollector, Locale.getDefault(), UTF_8)); |
186 | | - fileManager.addSourceFiles(files); |
187 | | - classPath().ifPresent(path -> setLocation(fileManager, StandardLocation.CLASS_PATH, path)); |
188 | | - annotationProcessorPath() |
189 | | - .ifPresent( |
190 | | - path -> setLocation(fileManager, StandardLocation.ANNOTATION_PROCESSOR_PATH, path)); |
191 | | - CompilationTask task = |
192 | | - javaCompiler() |
193 | | - .getTask( |
194 | | - null, // use the default because old versions of javac log some output on stderr |
195 | | - fileManager, |
196 | | - diagnosticCollector, |
197 | | - options(), |
198 | | - ImmutableSet.<String>of(), |
199 | | - files); |
200 | | - task.setProcessors(processors()); |
201 | | - boolean succeeded = task.call(); |
202 | | - Compilation compilation = |
203 | | - new Compilation( |
204 | | - this, |
205 | | - files, |
206 | | - succeeded, |
207 | | - diagnosticCollector.getDiagnostics(), |
208 | | - fileManager.getOutputFiles()); |
209 | | - if (compilation.status().equals(Status.FAILURE) && compilation.errors().isEmpty()) { |
210 | | - throw new CompilationFailureException(compilation); |
| 184 | + try (StandardJavaFileManager standardFileManager = standardFileManager(diagnosticCollector); |
| 185 | + InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager(standardFileManager)) { |
| 186 | + fileManager.addSourceFiles(files); |
| 187 | + classPath().ifPresent(path -> setLocation(fileManager, StandardLocation.CLASS_PATH, path)); |
| 188 | + annotationProcessorPath() |
| 189 | + .ifPresent( |
| 190 | + path -> setLocation(fileManager, StandardLocation.ANNOTATION_PROCESSOR_PATH, path)); |
| 191 | + |
| 192 | + CompilationTask task = |
| 193 | + javaCompiler() |
| 194 | + .getTask( |
| 195 | + null, // use the default because old versions of javac log some output on stderr |
| 196 | + fileManager, |
| 197 | + diagnosticCollector, |
| 198 | + options(), |
| 199 | + ImmutableSet.<String>of(), |
| 200 | + files); |
| 201 | + task.setProcessors(processors()); |
| 202 | + boolean succeeded = task.call(); |
| 203 | + Compilation compilation = |
| 204 | + new Compilation( |
| 205 | + this, |
| 206 | + files, |
| 207 | + succeeded, |
| 208 | + diagnosticCollector.getDiagnostics(), |
| 209 | + fileManager.getOutputFiles()); |
| 210 | + if (compilation.status().equals(Status.FAILURE) && compilation.errors().isEmpty()) { |
| 211 | + throw new CompilationFailureException(compilation); |
| 212 | + } |
| 213 | + return compilation; |
| 214 | + } catch (IOException e) { |
| 215 | + throw new UncheckedIOException(e); |
211 | 216 | } |
212 | | - return compilation; |
| 217 | + } |
| 218 | + |
| 219 | + private StandardJavaFileManager standardFileManager( |
| 220 | + DiagnosticCollector<JavaFileObject> diagnosticCollector) { |
| 221 | + return javaCompiler().getStandardFileManager(diagnosticCollector, Locale.getDefault(), UTF_8); |
213 | 222 | } |
214 | 223 |
|
215 | 224 | @VisibleForTesting |
|
0 commit comments