4 Replies Latest reply on Nov 9, 2005 3:23 AM by Shigeru Chiba

    annotation bug?

    Zsolt Szloboda Newbie

      I have found the following strange behaviour in JavaAssist 3.1RC1:
      I have a method (myMethod)
      the method has an annotation (MyAnnotation)
      the annotation has a String[] element (value())

      if the element array is empty

      Object[] annotations = myMethod.getAnnotations()
      MyAnnotation myAnnotation = (MyAnnotation)annotations;
      String[] s = myAnnotation.value();

      throws an Exception, something like this:

      java.lang.reflect.UndeclaredThrowableException
      at $Proxy0.value(Unknown Source)
      at hu.idya.beanspy.Instrumentor.instrument(Instrumentor.java:92)
      at hu.idya.xul.test.Test.testInstrumentation(Test.java:81)
      at hu.idya.xul.test.Test.main(Test.java:25)
      Caused by: java.lang.ClassNotFoundException: no array elements specified
      at javassist.bytecode.annotation.ArrayMemberValue.getValue(ArrayMemberValue.java:54)
      at javassist.bytecode.annotation.AnnotationImpl.getDefault(AnnotationImpl.java:79)
      at javassist.bytecode.annotation.AnnotationImpl.invoke(AnnotationImpl.java:59)
      ... 4 more

        • 1. Re: annotation bug?
          Shigeru Chiba Expert

          Thank you for a bug report.
          I have fixed this bug. Please download
          the latest snapshot from CVS.

          • 2. Re: annotation bug?
            Jean Safar Newbie

            Hi,

            Along the same lines, I am trying to create a class and Annotations on the fly to it. When I finally ask the Class descriptor to give me the annotations it does not find it. Am I doing something wrong here?

            CtClass c = pool.makeClass("NewClass");
             ClassFile cf = c.getClassFile();
             ConstPool cp = cf.getConstPool();
             AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
             javassist.bytecode.annotation.Annotation a = new javassist.bytecode.annotation.Annotation("MyAnnotation", cp);
             a.addMemberValue("name", new StringMemberValue("Chiba", cp));
             a.addMemberValue("version", new StringMemberValue("Chiba", cp));
             a.addMemberValue("description", new StringMemberValue("Chiba", cp));
             a.addMemberValue("interfaceName", new StringMemberValue("Chiba", cp));
             attr.setAnnotation(a);
             System.out.println(attr);
             cf.addAttribute(attr);
             Object [] ans = c.getAnnotations() ;
             System.out.println("Num Annotation : " +ans.length);
             Class newclass = c.toClass();
             Annotation[] anns = newclass.getAnnotations();
             System.out.println("Num NewClass Annotation : " +anns.length);



            • 3. Re: annotation bug?
              Jean Safar Newbie

               

              "safarje" wrote:
              Hi, (sorry for the repost but the original got truncated...)

              Along the same lines, I am trying to create a class and Annotations on the fly to it. When I finally ask the Class descriptor to give me the annotations it does not find it. Am I doing something wrong here?
              <p>The result is:
              @MyAnnotation(description="Chiba", interfaceName="Chiba", name="Chiba", version="Chiba")
              Num Annotation : 1
              Num NewClass Annotation : 0
              <p>
              <p>Is it possible to define annotations on the fly like I did? (I am using javassist 3.1).

              Best Regards
              Jean
              CtClass c = pool.makeClass("NewClass");
               ClassFile cf = c.getClassFile();
               ConstPool cp = cf.getConstPool();
               AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag);
               javassist.bytecode.annotation.Annotation a = new javassist.bytecode.annotation.Annotation("MyAnnotation", cp);
               a.addMemberValue("name", new StringMemberValue("Chiba", cp));
               a.addMemberValue("version", new StringMemberValue("Chiba", cp));
               a.addMemberValue("description", new StringMemberValue("Chiba", cp));
               a.addMemberValue("interfaceName", new StringMemberValue("Chiba", cp));
               attr.setAnnotation(a);
               System.out.println(attr);
               cf.addAttribute(attr);
               Object [] ans = c.getAnnotations() ;
               System.out.println("Num Annotation : " +ans.length);
               Class newclass = c.toClass();
               Annotation[] anns = newclass.getAnnotations();
               System.out.println("Num NewClass Annotation : " +anns.length);



              • 4. Re: annotation bug?
                Shigeru Chiba Expert

                Oh, you must insert the following code before calling c.toClass():

                cf.setMajorVersion(49);
                cf.setMinorVersion(0);


                I will add this info to the javadoc comment... Thanks.