|
20 | 20 |
|
21 | 21 | import com.google.auto.service.AutoService;
|
22 | 22 | import com.google.gson.Gson;
|
| 23 | +import java.io.BufferedWriter; |
23 | 24 | import java.io.IOException;
|
| 25 | +import java.io.OutputStream; |
24 | 26 | import java.io.OutputStreamWriter;
|
25 | 27 | import java.io.PrintWriter;
|
| 28 | +import java.io.UncheckedIOException; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
26 | 31 | import java.util.Set;
|
27 | 32 | import javax.annotation.processing.AbstractProcessor;
|
28 | 33 | import javax.annotation.processing.ProcessingEnvironment;
|
@@ -51,42 +56,44 @@ public SourceVersion getSupportedSourceVersion() {
|
51 | 56 | return SourceVersion.latest();
|
52 | 57 | }
|
53 | 58 |
|
54 |
| - private final Gson gson = new Gson(); |
55 |
| - |
56 |
| - private PrintWriter pw; |
| 59 | + private final Map<String, BugPatternInstance> bugPatterns = new HashMap<>(); |
57 | 60 |
|
58 | 61 | /** {@inheritDoc} */
|
59 | 62 | @Override
|
60 | 63 | public synchronized void init(ProcessingEnvironment processingEnv) {
|
61 | 64 | super.init(processingEnv);
|
62 |
| - try { |
63 |
| - FileObject manifest = |
64 |
| - processingEnv |
65 |
| - .getFiler() |
66 |
| - .createResource(StandardLocation.SOURCE_OUTPUT, "", "bugPatterns.txt"); |
67 |
| - pw = new PrintWriter(new OutputStreamWriter(manifest.openOutputStream(), UTF_8), true); |
68 |
| - } catch (IOException e) { |
69 |
| - throw new RuntimeException(e); |
70 |
| - } |
71 | 65 | }
|
72 | 66 |
|
73 | 67 | /** {@inheritDoc} */
|
74 | 68 | @Override
|
75 | 69 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
76 | 70 | for (Element element : roundEnv.getElementsAnnotatedWith(BugPattern.class)) {
|
77 |
| - gson.toJson(BugPatternInstance.fromElement(element), pw); |
78 |
| - pw.println(); |
| 71 | + BugPatternInstance bugPattern = BugPatternInstance.fromElement(element); |
| 72 | + bugPatterns.put(bugPattern.name, bugPattern); |
79 | 73 | }
|
80 | 74 |
|
81 | 75 | if (roundEnv.processingOver()) {
|
82 |
| - // this was the last round, do cleanup |
83 |
| - cleanup(); |
| 76 | + try { |
| 77 | + FileObject manifest = |
| 78 | + processingEnv |
| 79 | + .getFiler() |
| 80 | + .createResource(StandardLocation.SOURCE_OUTPUT, "", "bugPatterns.txt"); |
| 81 | + try (OutputStream os = manifest.openOutputStream(); |
| 82 | + PrintWriter pw = |
| 83 | + new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, UTF_8)))) { |
| 84 | + Gson gson = new Gson(); |
| 85 | + bugPatterns.entrySet().stream() |
| 86 | + .sorted(Map.Entry.comparingByKey()) |
| 87 | + .forEachOrdered( |
| 88 | + e -> { |
| 89 | + gson.toJson(e.getValue(), pw); |
| 90 | + pw.println(); |
| 91 | + }); |
| 92 | + } |
| 93 | + } catch (IOException e) { |
| 94 | + throw new UncheckedIOException(e); |
| 95 | + } |
84 | 96 | }
|
85 | 97 | return false;
|
86 | 98 | }
|
87 |
| - |
88 |
| - /** Perform cleanup after last round of annotation processing. */ |
89 |
| - private void cleanup() { |
90 |
| - pw.close(); |
91 |
| - } |
92 | 99 | }
|
0 commit comments