Navigation Menu

Skip to content

Commit

Permalink
8308116: jdk.test.lib.compiler.InMemoryJavaCompiler.compile does not …
Browse files Browse the repository at this point in the history
…close files

Reviewed-by: lucy
Backport-of: e9320f31dcc4ff5197e8c3bca504a7d5c1a9035e
  • Loading branch information
GoeLin committed Oct 11, 2023
1 parent 5e0be48 commit d00e1a3
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2017, 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
Expand Down Expand Up @@ -32,10 +32,11 @@
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;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
Expand Down Expand Up @@ -167,25 +168,12 @@ public boolean hasLocation(Location location) {
* @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) {
Expand All @@ -195,6 +183,19 @@ private static CompilationTask getCompilationTask(MemoryJavaFileObject file, Str
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();
}
}

1 comment on commit d00e1a3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.