8308116: jdk.test.lib.compiler.InMemoryJavaCompiler.compile does not close files
Reviewed-by: lmesnik, stefank, jlahoda
This commit is contained in:
parent
97d3b2731e
commit
e9320f31dc
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -32,8 +32,8 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.tools.ForwardingJavaFileManager;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.ForwardingJavaFileManager;
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.JavaCompiler.CompilationTask;
|
||||
import javax.tools.JavaFileManager;
|
||||
@ -168,25 +168,12 @@ public class InMemoryJavaCompiler {
|
||||
* @param className The name of the class
|
||||
* @param sourceCode The source code for the class with name {@code className}
|
||||
* @param options additional command line options
|
||||
* @throws RuntimeException if the compilation did not succeed
|
||||
* @throws RuntimeException if the compilation did not succeed or if closing
|
||||
* the {@code JavaFileManager} used for the compilation did not succeed
|
||||
* @return The resulting byte code from the compilation
|
||||
*/
|
||||
public static byte[] compile(String className, CharSequence sourceCode, String... options) {
|
||||
MemoryJavaFileObject file = new MemoryJavaFileObject(className, sourceCode);
|
||||
CompilationTask task = getCompilationTask(file, options);
|
||||
|
||||
if(!task.call()) {
|
||||
throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
|
||||
}
|
||||
|
||||
return file.getByteCode();
|
||||
}
|
||||
|
||||
private static JavaCompiler getCompiler() {
|
||||
return ToolProvider.getSystemJavaCompiler();
|
||||
}
|
||||
|
||||
private static CompilationTask getCompilationTask(MemoryJavaFileObject file, String... options) {
|
||||
List<String> opts = new ArrayList<>();
|
||||
String moduleOverride = null;
|
||||
for (String opt : options) {
|
||||
@ -196,6 +183,19 @@ public class InMemoryJavaCompiler {
|
||||
opts.add(opt);
|
||||
}
|
||||
}
|
||||
return getCompiler().getTask(null, new FileManagerWrapper(file, moduleOverride), null, opts, null, Arrays.asList(file));
|
||||
try (JavaFileManager fileManager = new FileManagerWrapper(file, moduleOverride)) {
|
||||
CompilationTask task = getCompiler().getTask(null, fileManager, null, opts, null, Arrays.asList(file));
|
||||
if (!task.call()) {
|
||||
throw new RuntimeException("Could not compile " + className + " with source code " + sourceCode);
|
||||
}
|
||||
|
||||
return file.getByteCode();
|
||||
} catch (IOException ioe) {
|
||||
throw new RuntimeException(ioe);
|
||||
}
|
||||
}
|
||||
|
||||
private static JavaCompiler getCompiler() {
|
||||
return ToolProvider.getSystemJavaCompiler();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user