From 39662804c334d11cd3caecc5d056759fbfd74b1a Mon Sep 17 00:00:00 2001 From: clayb Date: Sat, 21 Sep 2019 09:23:59 -0700 Subject: [PATCH] Suppress emitting errors about unprocessed elements when there are already errors in the processing round. The extra errors don't add any information and end up hiding the real errors. I found this substantially reduced the issue highlighted in b/141176717. RELNOTES=Suppress error noise in `com.google.auto.common.BasicAnnotationProcessor` ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=270454309 --- .../auto/common/BasicAnnotationProcessor.java | 8 ++- .../common/BasicAnnotationProcessorTest.java | 49 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java b/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java index 2d9c1199b0..8de3b817ad 100644 --- a/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java +++ b/common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java @@ -171,10 +171,14 @@ public final boolean process(Set annotations, RoundEnviro deferredElementNames.clear(); - // If this is the last round, report all of the missing elements + // If this is the last round, report all of the missing elements if there + // were no errors raised in the round; otherwise reporting the missing + // elements just adds noise the output. if (roundEnv.processingOver()) { postRound(roundEnv); - reportMissingElements(deferredElements, elementsDeferredBySteps.values()); + if (!roundEnv.errorRaised()) { + reportMissingElements(deferredElements, elementsDeferredBySteps.values()); + } return false; } diff --git a/common/src/test/java/com/google/auto/common/BasicAnnotationProcessorTest.java b/common/src/test/java/com/google/auto/common/BasicAnnotationProcessorTest.java index 0cf1075d08..59a135c429 100644 --- a/common/src/test/java/com/google/auto/common/BasicAnnotationProcessorTest.java +++ b/common/src/test/java/com/google/auto/common/BasicAnnotationProcessorTest.java @@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat; import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources; +import static javax.tools.Diagnostic.Kind.ERROR; import static javax.tools.StandardLocation.SOURCE_OUTPUT; import com.google.common.collect.ImmutableList; @@ -166,6 +167,37 @@ public Set> annotations() { } } + /** An annotation which causes an annotation processing error. */ + public @interface CauseError {} + + /** Report an error for any class annotated. */ + public static class CauseErrorProcessor extends BasicAnnotationProcessor { + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + protected Iterable initSteps() { + return ImmutableSet.of( + new ProcessingStep() { + @Override + public Set process( + SetMultimap, Element> elementsByAnnotation) { + for (Element e : elementsByAnnotation.values()) { + processingEnv.getMessager().printMessage(ERROR, "purposeful error", e); + } + return ImmutableSet.copyOf(elementsByAnnotation.values()); + } + + @Override + public Set> annotations() { + return ImmutableSet.of(CauseError.class); + } + }); + } + } + @Test public void properlyDefersProcessing_typeElement() { JavaFileObject classAFileObject = JavaFileObjects.forSourceLines("test.ClassA", "package test;", @@ -329,6 +361,23 @@ Correspondence, SetMultimap> setMultimapValuesByStr .in(classAFileObject).onLine(4); } + @Test + public void reportsMissingTypeSuppressedWhenOtherErrors() { + JavaFileObject classAFileObject = + JavaFileObjects.forSourceLines( + "test.ClassA", + "package test;", + "", + "@" + CauseError.class.getCanonicalName(), + "public class ClassA {}"); + assertAbout(javaSources()) + .that(ImmutableList.of(classAFileObject)) + .processedWith(new CauseErrorProcessor()) + .failsToCompile() + .withErrorCount(1) + .withErrorContaining("purposeful"); + } + private static void generateClass(Filer filer, String generatedClassName) { PrintWriter writer = null; try {