Skip to content

Commit

Permalink
Suppress emitting errors about unprocessed elements when there are al…
Browse files Browse the repository at this point in the history
…ready 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
  • Loading branch information
sir-buckyball authored and netdpb committed Sep 23, 2019
1 parent eadfe42 commit 3966280
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,14 @@ public final boolean process(Set<? extends TypeElement> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -166,6 +167,37 @@ public Set<? extends Class<? extends Annotation>> 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<? 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;",
Expand Down Expand Up @@ -329,6 +361,23 @@ Correspondence<SetMultimap<K, V>, SetMultimap<K, String>> 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 {
Expand Down

0 comments on commit 3966280

Please sign in to comment.