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

Method invocation as annotation value: annotation processing does not run anymore

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P2 P2
    • None
    • 10
    • tools

      Let's consider following code:

      @interface TestInter {
          int id();
      }
       
      public class AnnotClass {
          @testInter(id = Integer(123)) public void m(){
          }
      }

      When compiling this code with annotation processor by javac from jdk10 build 9 annotation processing isn't run, just an error message is output:

      AnnotClass.java:6: error: expression not allowed as annotation value
          @testInter(id = Integer(123)) public void m(){
                                 ^
      1 error

      However when compiling this code with annotation processor by javac from jdk9 build 181 annotation processing is performed, please see the output below:

      Root elemnts count: 2
        TestInter
          id()
        AnnotClass
          AnnotClass()
          m()
            @testInter(id=<error>)
              id() = <error> (class java.lang.String)
      Root elemnts count: 0
      AnnotClass.java:6: error: cannot find symbol
          @testInter(id = Integer(123)) public void m(){
                          ^
        symbol: method Integer(int)
        location: class AnnotClass
      1 error

      Annotation processor code is presented below:

      import javax.annotation.processing.*;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.*;
      import java.util.*;

      @SupportedAnnotationTypes("*")
      public class TestProc extends AbstractProcessor {
          @Override
          public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
              System.out.println("Root elemnts count: " + roundEnv.getRootElements().size());
              for (Element el: roundEnv.getRootElements()) {
                  System.out.println(" " + el);
                  for (Element e2: el.getEnclosedElements()) {
                      System.out.println(" " + e2);
                      for (AnnotationMirror am: e2.getAnnotationMirrors()) {
                          System.out.println(" " + am);
                          for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> en: am.getElementValues().entrySet()) {
                              System.out.println(" " + en.getKey() + " = " + en.getValue().getValue() + " (" + en.getValue().getValue().getClass() + ")");
                          }
                      }
                  }
              }
              return false;
          }

          public SourceVersion getSupportedSourceVersion() {
              return SourceVersion.latest();
          }
      }

      The minimized test case is attached, in order to run it, please:
      1) Unzip attached archive to some Windows machine.
      2) Rename test\run_bat to test\run.bat.
      3) Modify test\run.bat by assigning the full path to your JDK installation to JDK_HOME environment variable.
      4) Execute run.bat.

            sadayapalam Srikanth Adayapalam (Inactive)
            grakov Georgiy Rakov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: