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

    Details

    • Type: Bug
    • Status: New
    • Priority: P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: tools

      Description

      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

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              cushon Liam Miller-Cushon
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: