Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a close() method to Compilation results #370

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 15 additions & 2 deletions src/main/java/com/google/testing/compile/Compilation.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import java.io.Closeable;
import java.io.IOException;
import java.util.Optional;
import java.util.stream.Collector;
Expand All @@ -38,25 +40,28 @@
import javax.tools.JavaFileObject;

/** The results of {@linkplain Compiler#compile compiling} source files. */
public final class Compilation {
public final class Compilation implements Closeable {

private final Compiler compiler;
private final ImmutableList<JavaFileObject> sourceFiles;
private final Status status;
private final ImmutableList<Diagnostic<? extends JavaFileObject>> diagnostics;
private final ImmutableList<JavaFileObject> generatedFiles;
private final Closeable fileCloseable;

Compilation(
Compiler compiler,
Iterable<? extends JavaFileObject> sourceFiles,
boolean successful,
Iterable<Diagnostic<? extends JavaFileObject>> diagnostics,
Iterable<JavaFileObject> generatedFiles) {
Iterable<JavaFileObject> generatedFiles,
Closeable fileCloseable) {
this.compiler = compiler;
this.sourceFiles = ImmutableList.copyOf(sourceFiles);
this.status = successful ? Status.SUCCESS : Status.FAILURE;
this.diagnostics = ImmutableList.copyOf(diagnostics);
this.generatedFiles = ImmutableList.copyOf(generatedFiles);
this.fileCloseable = fileCloseable;
}

/** The compiler. */
Expand Down Expand Up @@ -218,6 +223,14 @@ public String toString() {
return builder.toString();
}

/**
* Method for closing any underlying file manager resources.
*/
@Override
public void close() throws IOException {
fileCloseable.close();
}

/** Returns a description of the why the compilation failed. */
String describeFailureDiagnostics() {
ImmutableList<Diagnostic<? extends JavaFileObject>> diagnostics = diagnostics();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/google/testing/compile/Compiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.io.Closeables;
import com.google.testing.compile.Compilation.Status;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -205,8 +206,12 @@ public final Compilation compile(Iterable<? extends JavaFileObject> files) {
files,
succeeded,
diagnosticCollector.getDiagnostics(),
fileManager.getOutputFiles());
fileManager.getOutputFiles(),
fileManager);
if (compilation.status().equals(Status.FAILURE) && compilation.errors().isEmpty()) {
try {
fileManager.close();
} catch (Throwable ignored) {}
throw new CompilationFailureException(compilation);
}
return compilation;
Expand Down