Skip to content

Commit

Permalink
Test double closing of outputstream
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 551115792
  • Loading branch information
dx404 authored and Error Prone Team committed Jul 26, 2023
1 parent 77ba705 commit 52083a2
Showing 1 changed file with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
Expand Down Expand Up @@ -55,24 +55,23 @@ public SourceVersion getSupportedSourceVersion() {

private PrintWriter pw;

/** {@inheritDoc} */
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
try {
FileObject manifest =
processingEnv
.getFiler()
.createResource(StandardLocation.SOURCE_OUTPUT, "", "bugPatterns.txt");
pw = new PrintWriter(new OutputStreamWriter(manifest.openOutputStream(), UTF_8), true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

/** {@inheritDoc} */
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// Initializes field pw in the first round instead of the init method to ensure the created
// PrintWriter instance is always closed during cleanup.
if (pw == null) {
try {
FileObject manifest =
processingEnv
.getFiler()
.createResource(StandardLocation.SOURCE_OUTPUT, "", "bugPatterns.txt");
pw = new PrintWriter(new OutputStreamWriter(manifest.openOutputStream(), UTF_8), true);
} catch (IOException e) {
cleanup();
throw new UncheckedIOException(e);
}
}
for (Element element : roundEnv.getElementsAnnotatedWith(BugPattern.class)) {
gson.toJson(BugPatternInstance.fromElement(element), pw);
pw.println();
Expand Down

0 comments on commit 52083a2

Please sign in to comment.