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

Javac fails to compile annotations when annotation processor is used

XMLWordPrintable

    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Server VM (build 1.6.0-b105, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux magic 2.6.17.13 #1 SMP Mon Dec 11 15:16:08 EST 2006 i686 AMD Opteron(tm) Processor 852 AuthenticAMD GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      javac fails to compile any any annotation with an element default value that references a constant in another class or interface. However, compilation succeeds without processing annotations

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Try to compile an annotation with a default value for an element that references a constant in another class.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      javac should compile the annotation class without an error.
      ACTUAL -
      javac will fail with an error message of the form:
      found : <element type>
      required: <element type>
      <element type> <element name>() default <constant expression that references another class or interface>;

      for example,

      package test;

      public class TestConstants {

          public static final String DEFAULT = "Test";

      }
      package test;

      public @interface Test {

         String x() default TestConstants.DEFAULT

      }

      results in:

      found : java.lang.String
      required: java.lang.String
           String x() default TestConstants.DEFAULT;

      when compiled with any annotation processor.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      found : java.lang.String
      required: java.lang.String
           String x() default TestConstants.DEFAULT;


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package test;

      import java.util.Set;

      import javax.annotation.processing.AbstractProcessor;
      import javax.annotation.processing.RoundEnvironment;
      import javax.annotation.processing.SupportedAnnotationTypes;
      import javax.annotation.processing.SupportedOptions;
      import javax.annotation.processing.SupportedSourceVersion;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.TypeElement;

      @SupportedOptions(value = {})
      @SupportedSourceVersion(value = SourceVersion.RELEASE_6)
      @SupportedAnnotationTypes(value = {"test.Test"})
      public class TestProcessor extends AbstractProcessor {

      public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
      throw new UnsupportedOperationException();
      }

      }
      ---------------------------------------------------------------------------------------
      package test;

      public @interface Test {

      String x() default TestConstants.DEFAULT;

      }
      ---------------------------------------------------------------------------------------
      package test;

      public class TestConstants {

      public static final String DEFAULT = "test";

      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      You must use only hardcoded constants. For example, change the definition of x to

      String x() default "test" works fine.

      Perhaps even worse, however, is that you can never have default values that are enumerations.

            darcy Joe Darcy
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: