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

Annotations with 'Class[] value()' get their values lost

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • tools
    • x86
    • linux

      FULL PRODUCT VERSION :
      atom 11:37:26 ~> java -version
      java version "1.6.0_01"
      Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Linux atom.opsecsecurity.com 2.6.9-42.0.10.ELsmp #1 SMP Tue Feb 27 10:11:19 EST 2007 i686 i686 i386 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      Calling getValue() on an annotation for which getValue() is supposed to return a Class[] throws an exception. This is known as reported in http://forum.java.sun.com/thread.jspa?threadID=791053&tstart=0 but the problem is that getTypeMirror() as successted in the posting only returns the first element of the array and not the entire array. Consequently, if I have an annotation that receives an array of Class such as
      @SomeAnnotation(Iterable.class, Object.class, MyClass.class)
      the only value that I can access by calling getTypeMirror() is the first value of the array (Iterable.class) and not the subsequent ones.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. javac AnnotationBug.java
      2. javac -processor AnnotationBug -processorpath . AnnotationBug.java


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.lang.annotation.*;
      import javax.annotation.*;
      import javax.annotation.processing.*;

      import java.io.*;
      import java.util.*;

      import javax.annotation.processing.*;
      import javax.lang.model.SourceVersion;
      import javax.lang.model.element.*;
      import javax.lang.model.type.*;
      import javax.tools.Diagnostic;

      @interface Bug{
         Class[] value();
      }

      ////////
      // to compile:
      // javac AnnotationBug.java
      // then again using the processor
      // javac -processor AnnotationBug -processorpath . AnnotationBug.java

      @Bug({Object.class, Iterable.class, Byte.class, Appendable.class, Runnable.class, Boolean.class})
      @SupportedAnnotationTypes("Bug")
      public class AnnotationBug extends AbstractProcessor {

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

         @Override
         public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment round) {

            for (Element element : round.getElementsAnnotatedWith(Bug.class)){
               Bug bug = element.getAnnotation(Bug.class);

               try{
                 // The line below is expected to blow up as per http://forum.java.sun.com/thread.jspa?threadID=791053&tstart=0
                 Class[] classes = bug.value();
               }
               catch(MirroredTypeException e){
                 if (e.getTypeMirror() instanceof ArrayType)
                    System.out.println("This is what is expected and make some sense.");
                 else{
                    System.out.println("This is not expected, and we can only access the first value in the array contained in Bug");
                    System.out.println("We can only access " + e.getTypeMirror());
                    // the problem is that while we declared an array of classes and passed that to the
                    // annotation (line 25), we can only access the first element of that array and all other
                    // elements are not accessible.
                 }
               }
            }

            return true;
         }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      none known at this time

            jjg Jonathan Gibbons
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: