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

Use annotation generated constant indirectly in annotation will lead error

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      For example, I have an annotation processor. It generates a simple java source file with a constant

      public interface HelloWorldMessage {
        String HELLO_WORLD = "Hello World";
      }

      Then I use the value in annotation

      public class UseHelloWorld {
        public static final String HW = HelloWorldMessage.HELLO_WORLD;

        // @Anno(UseHelloWorld.HW) // This line will lead error
        @Anno(HelloWorldMessage.HELLO_WORLD) // This line is ok
        public void bean() {
        }
      }

      If I use the value indrectly(the first line `@Anno(UseHelloWorld.HW)`), javac will give compiler error that symbol not find. It seems javac skips annotation processing.


      REGRESSION : Last worked in version 8u162

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      * You can refer this link https://gist.github.com/XDean/29a225f631cefbc75752b6a35b5ccb28 for sample code

      1. Create and copy sources below
      2. Compile the 'processor' folder
      3. Compile the 'test' folder with classpath to processor folder

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Complier should do annotation processing and compile success. (like directly use the constant)
      ACTUAL -
      javac will give following error

      UseHelloWorld.java:2: error: cannot find symbol
        public static final String HW = HelloWorldMessage.HELLO_WORLD;
                                        ^
        symbol: variable HelloWorldMessage
        location: class UseHelloWorld
      UseHelloWorld.java:4: error: element value must be a constant expression
        @Anno(UseHelloWorld.HW)
                           ^
      2 errors

      That means the annotation processor is skipped.

      ---------- BEGIN SOURCE ----------
      * You can refer this link https://gist.github.com/XDean/29a225f631cefbc75752b6a35b5ccb28 for sample code

      File structure

      - processor
        - HelloWorldProcessor.java
      - test
        - META-INF
          - services
            - javax.annotation.processing.Processor
        - Anno.java
        - UseHelloWorld.java

      Sources

      * HelloWorldProcessor.java

      import java.io.IOException;
      import java.io.PrintWriter;
      import java.util.Set;

      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.Processor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.annotation.processing.SupportedSourceVersion;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.TypeElement;
      import javax.tools.FileObject;

      @SupportedAnnotationTypes("*")
      @SupportedSourceVersion(SourceVersion.RELEASE_8)
      public class HelloWorldProcessor extends AbstractProcessor {

        @Override
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
          if (!roundEnv.processingOver()) {
            try {
              FileObject resource = processingEnv.getFiler().createSourceFile("xdean.processor.HelloWorldMessage");
              PrintWriter ps = new PrintWriter(resource.openWriter());
              ps.println("public interface HelloWorldMessage { String HELLO_WORLD = \"Hello World\";}");
              ps.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
          return false;
        }
      }

      * javax.annotation.processing.Processor

      HelloWorldProcessor

      * Anno.java

      public @interface Anno {
        String value();
      }

      * UseHelloWorld.java
      public class UseHelloWorld {
        public static final String HW = HelloWorldMessage.HELLO_WORLD;

        @Anno(UseHelloWorld.HW)
        public void bean() {
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Do not indirect use annotation processor generated constants.

      FREQUENCY : always


        1. UseHelloWorld.java
          0.2 kB
        2. HelloWorldProcessor.java
          1 kB
        3. Anno.java
          0.0 kB

            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: