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: mbaesken
Backport-of: e9320f31dcc4ff5197e8c3bca504a7d5c1a9035e
  • Loading branch information
Amos Shi authored and GoeLin committed Dec 30, 2023
1 parent 4f91f8f commit 2da1ac7
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions test/lib/jdk/test/lib/compiler/InMemoryJavaCompiler.java
Original file line number Diff line number Diff line change
@@ -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 @@ -106,7 +107,7 @@ public String getClassName() {
}
}

private static class FileManagerWrapper extends ForwardingJavaFileManager {
private static class FileManagerWrapper extends ForwardingJavaFileManager<JavaFileManager> {
private static final Location PATCH_LOCATION = new Location() {
@Override
public String getName() {
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 2da1ac7

@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.