aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorclayb <clayb@google.com>2019-09-21 09:23:59 -0700
committerDavid P. Baker <dpb@google.com>2019-09-23 11:26:19 -0700
commit39662804c334d11cd3caecc5d056759fbfd74b1a (patch)
tree3baa3ac7c712ec0477f9ea0effec9f8802540204 /common
parenteadfe4220b2493b765860fa64b675b6ce197d063 (diff)
downloadauto-39662804c334d11cd3caecc5d056759fbfd74b1a.tar.gz
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
Diffstat (limited to 'common')
-rw-r--r--common/src/main/java/com/google/auto/common/BasicAnnotationProcessor.java8
-rw-r--r--common/src/test/java/com/google/auto/common/BasicAnnotationProcessorTest.java49
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 2d9c1199..8de3b817 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 abstract class BasicAnnotationProcessor extends AbstractProcessor {
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 0cf1075d..59a135c4 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.assertAbout;
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 class BasicAnnotationProcessorTest {
}
}
+ /** 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<? extends ProcessingStep> initSteps() {
+ return ImmutableSet.of(
+ new ProcessingStep() {
+ @Override
+ public Set<Element> process(
+ SetMultimap<Class<? extends Annotation>, Element> elementsByAnnotation) {
+ for (Element e : elementsByAnnotation.values()) {
+ processingEnv.getMessager().printMessage(ERROR, "purposeful error", e);
+ }
+ return ImmutableSet.copyOf(elementsByAnnotation.values());
+ }
+
+ @Override
+ public Set<? extends Class<? extends Annotation>> annotations() {
+ return ImmutableSet.of(CauseError.class);
+ }
+ });
+ }
+ }
+
@Test public void properlyDefersProcessing_typeElement() {
JavaFileObject classAFileObject = JavaFileObjects.forSourceLines("test.ClassA",
"package test;",
@@ -329,6 +361,23 @@ public class BasicAnnotationProcessorTest {
.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 {