Skip to content

Commit d00e1a3

Browse files
committed
8308116: jdk.test.lib.compiler.InMemoryJavaCompiler.compile does not close files
Reviewed-by: lucy Backport-of: e9320f31dcc4ff5197e8c3bca504a7d5c1a9035e
1 parent 5e0be48 commit d00e1a3

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,10 +32,11 @@
3232
import java.util.Arrays;
3333
import java.util.List;
3434

35-
import javax.tools.ForwardingJavaFileManager;
3635
import javax.tools.FileObject;
36+
import javax.tools.ForwardingJavaFileManager;
3737
import javax.tools.JavaCompiler;
3838
import javax.tools.JavaCompiler.CompilationTask;
39+
import javax.tools.JavaFileManager;
3940
import javax.tools.JavaFileObject;
4041
import javax.tools.JavaFileObject.Kind;
4142
import javax.tools.SimpleJavaFileObject;
@@ -167,25 +168,12 @@ public boolean hasLocation(Location location) {
167168
* @param className The name of the class
168169
* @param sourceCode The source code for the class with name {@code className}
169170
* @param options additional command line options
170-
* @throws RuntimeException if the compilation did not succeed
171+
* @throws RuntimeException if the compilation did not succeed or if closing
172+
* the {@code JavaFileManager} used for the compilation did not succeed
171173
* @return The resulting byte code from the compilation
172174
*/
173175
public static byte[] compile(String className, CharSequence sourceCode, String... options) {
174176
MemoryJavaFileObject file = new MemoryJavaFileObject(className, sourceCode);
175-
CompilationTask task = getCompilationTask(file, options);
176-
177-
if(!task.call()) {
178-
throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
179-
}
180-
181-
return file.getByteCode();
182-
}
183-
184-
private static JavaCompiler getCompiler() {
185-
return ToolProvider.getSystemJavaCompiler();
186-
}
187-
188-
private static CompilationTask getCompilationTask(MemoryJavaFileObject file, String... options) {
189177
List<String> opts = new ArrayList<>();
190178
String moduleOverride = null;
191179
for (String opt : options) {
@@ -195,6 +183,19 @@ private static CompilationTask getCompilationTask(MemoryJavaFileObject file, Str
195183
opts.add(opt);
196184
}
197185
}
198-
return getCompiler().getTask(null, new FileManagerWrapper(file, moduleOverride), null, opts, null, Arrays.asList(file));
186+
try (JavaFileManager fileManager = new FileManagerWrapper(file, moduleOverride)) {
187+
CompilationTask task = getCompiler().getTask(null, fileManager, null, opts, null, Arrays.asList(file));
188+
if (!task.call()) {
189+
throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
190+
}
191+
192+
return file.getByteCode();
193+
} catch (IOException ioe) {
194+
throw new RuntimeException(ioe);
195+
}
196+
}
197+
198+
private static JavaCompiler getCompiler() {
199+
return ToolProvider.getSystemJavaCompiler();
199200
}
200201
}

0 commit comments

Comments
 (0)