Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8268575

Annotations not visible on model elements before they are generated

    XMLWordPrintable

Details

    • Bug
    • Status: Resolved
    • P4
    • Resolution: Fixed
    • None
    • 18
    • tools
    • b27

    Description

      After JDK-8206325, annotations whose symbols can't be resolved during the current processing round are not returned by getAnnotationMirrors(). I noticed this investigating an annotation processor that stopped working with the new behaviour, because it assumes a generated annotation will be returned by getAnnotationMirrors on the first processing round, before the annotation is actually generated.

      I think it's somewhat subjective whether this is a bug, there are cases where the processing model can't return information when errors are present, but in this case I wonder if it would be better to include the erroneous annotation in the result of getAnnotationMirrors, and let clients check if the annotation is erroneous?

      A possible fix is to modify the check in Annotation.annotateNow to do something like:

      -if (a.type.tsym.isAnnotationType()) {
      +if (a.type.isErroneous() || a.type.tsym.isAnnotationType()) {

      Repro:

      ===
      import java.io.IOException;
      import java.io.UncheckedIOException;
      import java.io.Writer;
      import java.util.Set;
      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.Element;
      import javax.lang.model.element.ExecutableElement;
      import javax.lang.model.element.TypeElement;
      import javax.lang.model.element.VariableElement;
      import javax.tools.Diagnostic.Kind;
      import javax.tools.JavaFileObject;

      @SupportedAnnotationTypes("*")
      public class P extends AbstractProcessor {

        @Override
        public SourceVersion getSupportedSourceVersion() {
          return SourceVersion.latestSupported();
        }

        int round = 1;

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
          processingEnv.getMessager().printMessage(Kind.NOTE, "round " + round);
          Element t = processingEnv.getElementUtils().getTypeElement("T");
          for (Element e : t.getEnclosedElements()) {
            if (e instanceof ExecutableElement) {
              for (VariableElement p : ((ExecutableElement) e).getParameters()) {
                processingEnv
                    .getMessager()
                    .printMessage(
                        Kind.NOTE, "annotations on " + p + ": " + p.getAnnotationMirrors().toString());
              }
            }
          }
          if (round == 1) {
            String name = "A";
            try {
              JavaFileObject jfo = processingEnv.getFiler().createSourceFile(name);
              try (Writer w = jfo.openWriter()) {
                w.write("@interface " + name + " {}");
              }
            } catch (IOException e) {
              throw new UncheckedIOException(e);
            }
          }
          round++;
          return false;
        }
      }
      ===

      ===
      class T {
        void f(@A int x) {}
      }
      ===

      With javac before JDK-8206325, the annotation @A on the parameter x is visible during the first processing round, before it has been generated:

      $ javac -fullversion -processor P T.java -implicit:none -s gen
      javac full version "12-ea+22"
      Note: round 1
      Note: annotations on x: @A
      Note: round 2
      Note: annotations on x: @A
      Note: round 3
      Note: annotations on x: @A

      After the fix, the annotation is dropped on the first round. A processor that visits x on the first round has no way of telling that x has a (currently erroneous) annotation, and might assume x is well formed but has no annotations:

      $ javac -fullversion -processor P T.java -implicit:none -s gen
      javac full version "12-ea+23"
      Note: round 1
      Note: annotations on x:
      Note: round 2
      Note: annotations on x: @A
      Note: round 3
      Note: annotations on x: @A

      Attachments

        Issue Links

          Activity

            People

              sadayapalam Srikanth Adayapalam
              cushon Liam Miller-Cushon
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: