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

AnnotationValues for unresolvable class literals are represented as the string "<error>" instead of an ErrorType

XMLWordPrintable

      Given an annotation element value that cannot be resolved (e.g. `@A(value = NoSuch.class)`), javac's implementation of AnnotationValue represents it using Attribute.UnresolvedClass.

      UnresolvedClass implements AnnotationValue#accept as AnnotationValueVisitor#visitString for the string value `"<error>"`: http://hg.openjdk.java.net/jdk/jdk/file/807d192fb7dd/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Attribute.java#l399

      Is this to spec? I am surprised it doesn't implement it using AnnotationValueVisitor#visitType with an javax.lang.model.type.ErrorType.

      ```
      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.AnnotationMirror;
      import javax.lang.model.element.AnnotationValue;
      import javax.lang.model.element.Element;
      import javax.lang.model.element.TypeElement;
      import javax.lang.model.util.SimpleAnnotationValueVisitor8;

      @interface A {
        Class<?> value() default Object.class;
      }

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

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

        @Override
        public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
          for (Element r : roundEnv.getRootElements()) {
            for (AnnotationMirror a : r.getAnnotationMirrors()) {
              System.err.println("annotation: " + a);
              for (AnnotationValue e : a.getElementValues().values()) {
                System.err.println("value: " + e);
                e.accept(
                    new SimpleAnnotationValueVisitor8<Void, Void>() {
                      @Override
                      public Void visitString(String s, Void aVoid) {
                        System.err.println("string value: " + s);
                        return null;
                      }
                    },
                    null);
              }
            }
          }
          return true;
        }
      }
      ```

      $ javac full version "14-ea+9-288"
      annotation: @A(<error>)
      value: <error>
      string value: <error>
      T.java:1: error: cannot find symbol
      @A(value = B.class)
                 ^
        symbol: class B
      1 error

            darcy Joe Darcy
            cushon Liam Miller-Cushon
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: