Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,34 @@ public class ForwardingJavaFileManager<M extends JavaFileManager> implements Jav
protected final M fileManager;

/**
* Creates a new instance of {@code ForwardingJavaFileManager}.
* Whether the delegate is owned by this instance and should be closed when
* this instance is closed.
*/
private final boolean shouldClose;

/**
* Creates a new instance of {@code ForwardingJavaFileManager} which takes
* ownership of the given delegate, and will close it when this instance is
* closed.
*
* @param fileManager delegate to this file manager
*/
protected ForwardingJavaFileManager(M fileManager) {
this(fileManager, true);
}

/**
* Creates a new instance of {@code ForwardingJavaFileManager}.
*
* @param fileManager delegate to this file manager
* @param shouldClose whether the delegate should be closed when this
* instance is closed.
*
* @since 26
*/
protected ForwardingJavaFileManager(M fileManager, boolean shouldClose) {
this.fileManager = Objects.requireNonNull(fileManager);
this.shouldClose = shouldClose;
}

/**
Expand Down Expand Up @@ -247,7 +270,9 @@ public void flush() throws IOException {

@Override
public void close() throws IOException {
fileManager.close();
if (shouldClose) {
fileManager.close();
}
}

/**
Expand Down
21 changes: 11 additions & 10 deletions test/langtools/tools/javac/classfiles/InnerClasses/T8068517.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,18 @@ void run() throws Exception {
void runTest(String aJava, String bJava) throws Exception {
try (JavaFileManager fm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null)) {
ToolBox tb = new ToolBox();
ToolBox.MemoryFileManager memoryFM1 = new ToolBox.MemoryFileManager(fm);
new JavacTask(tb).fileManager(memoryFM1)
.sources(aJava, bJava)
.run();
ToolBox.MemoryFileManager memoryFM2 = new ToolBox.MemoryFileManager(fm);
new JavacTask(tb).fileManager(memoryFM2)
.sources(bJava, aJava)
.run();
try (var memoryFM1 = new ToolBox.MemoryFileManager(fm, false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could leave this alone, but it seemed nicer to actually acknowledge that the file managers are closeable.

var memoryFM2 = new ToolBox.MemoryFileManager(fm, false)) {
new JavacTask(tb).fileManager(memoryFM1)
.sources(aJava, bJava)
.run();
new JavacTask(tb).fileManager(memoryFM2)
.sources(bJava, aJava)
.run();

Assert.check(Arrays.equals(memoryFM1.getFileBytes(StandardLocation.CLASS_OUTPUT, "B"),
memoryFM2.getFileBytes(StandardLocation.CLASS_OUTPUT, "B")));
Assert.check(Arrays.equals(memoryFM1.getFileBytes(StandardLocation.CLASS_OUTPUT, "B"),
memoryFM2.getFileBytes(StandardLocation.CLASS_OUTPUT, "B")));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import javax.tools.StandardLocation;
import toolbox.JavacTask;
import toolbox.TestRunner;
import toolbox.TestRunner.Test;
import toolbox.ToolBox;
import toolbox.ToolBox.MemoryFileManager;

Expand Down Expand Up @@ -174,7 +173,7 @@ private String getInfo(FileObject fo) {
};
try {
String generatedData;
try (MemoryFileManager mfm = new MemoryFileManager(sjfm)) {
try (MemoryFileManager mfm = new MemoryFileManager(sjfm, /* shouldClose */ false)) {
compiler.getTask(null, mfm, null, null, null,
List.of(new ToolBox.JavaSource("package test; public class Generated2 {}")))
.call();
Expand Down
8 changes: 5 additions & 3 deletions test/langtools/tools/lib/toolbox/ToolBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -856,17 +856,19 @@ private interface Content {
* and delegates to a default file manager for input files.
*/
public MemoryFileManager() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the vast majority of users, they don't care about the delegated instance and want it closed.

this(ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null));
this(ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null), true);
}

/**
* Constructs a memory file manager which stores output files in memory,
* and delegates to a specified file manager for input files.
*
* @param fileManager the file manager to be used for input files
* @param shouldClose whether the delegate file manager should be closed
* when this instance is closed
*/
public MemoryFileManager(JavaFileManager fileManager) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only called in 2 places, so migrating callers with an extra parameter is easy.

super(fileManager);
public MemoryFileManager(JavaFileManager fileManager, boolean shouldClose) {
super(fileManager, shouldClose);
files = new HashMap<>();
}

Expand Down