Skip to content

Commit 972ac1e

Browse files
cgdeckerCompile-Testing Team
authored andcommitted
Change Compiler.compile to close the StandardJavaFileManager it creates.
Fixes #370 According to that PR, it sounds like not closing this can leak file descriptors. RELNOTES=Fixed `Compiler.compile` to ensure that it doesn't leak file descriptors. PiperOrigin-RevId: 570424175
1 parent 7573767 commit 972ac1e

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

src/main/java/com/google/testing/compile/Compiler.java

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import javax.tools.JavaCompiler;
4444
import javax.tools.JavaCompiler.CompilationTask;
4545
import javax.tools.JavaFileObject;
46+
import javax.tools.StandardJavaFileManager;
4647
import javax.tools.StandardLocation;
4748
import org.checkerframework.checker.nullness.qual.Nullable;
4849

@@ -180,36 +181,44 @@ public final Compilation compile(JavaFileObject... files) {
180181
*/
181182
public final Compilation compile(Iterable<? extends JavaFileObject> files) {
182183
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);
211216
}
212-
return compilation;
217+
}
218+
219+
private StandardJavaFileManager standardFileManager(
220+
DiagnosticCollector<JavaFileObject> diagnosticCollector) {
221+
return javaCompiler().getStandardFileManager(diagnosticCollector, Locale.getDefault(), UTF_8);
213222
}
214223

215224
@VisibleForTesting

0 commit comments

Comments
 (0)