2 Replies Latest reply on May 29, 2006 6:35 AM by chiba

    rel 3.1 - Problem with CtBehavior#getParameterAnnotations

      The documentation of CtBehavior#getParameterAnnotations only states "@return an array of annotation-type objects".

      It doesn't mention what is returned when none of the parameters is annotated. However, most of the introspective methods mirror the ones in the regular reflection API, so one would expect it to behave like java.lang.Method#getParameterAnnotations.

      The following test case shows that it doesn't :

      import java.lang.annotation.Annotation;
      import java.lang.reflect.Method;
      
      import javassist.ClassPool;
      import javassist.CtClass;
      import javassist.CtMethod;
      import javassist.NotFoundException;
      import junit.framework.TestCase;
      
      public class TestCtMethod extends TestCase {
      
       public void test() throws NoSuchMethodException, NotFoundException, ClassNotFoundException {
       Method equalsReflection=Object.class.getMethod("equals", Object.class);
       Annotation[][] annotationsReflection=equalsReflection.getParameterAnnotations();
      
       ClassPool pool=ClassPool.getDefault();
       CtClass objectCtClass=pool.get("java.lang.Object");
       CtClass[] parameterTypes=new CtClass[1];
       parameterTypes[0]=pool.get("java.lang.Object");
       CtMethod equalsJavassist=objectCtClass.getDeclaredMethod("equals", parameterTypes);
       Object[][] annotationsJavassist=equalsJavassist.getParameterAnnotations();
      
       assertEquals(annotationsReflection.length, annotationsJavassist.length);
       }
      }
      


      Is the current behaviour really what was intended? In that case the documentation needs to be expanded and perhaps stress that it doesn't mirror its reflection API equivalent.

      Can one of the developers please confirm which it is? Thanks.


        • 1. Re: rel 3.1 - Problem with CtBehavior#getParameterAnnotation

          No, that is not intentional,

          "
          Method::getParameterAnnotations

          public Annotation[][] getParameterAnnotations()

          Returns an array of arrays that represent the annotations on the formal parameters, in declaration order, of the method represented by this Method object. (Returns an array of length zero if the underlying method is parameterless. If the method has one or more parameters, a nested array of length zero is returned for each parameter with no annotations.) The annotation objects contained in the returned arrays are serializable. The caller of this method is free to modify the returned arrays; it will have no effect on the arrays returned to other callers.
          "

          The cause is in AttributeInfo where if there are no
          parameter annotations at all for the behaviour, it doesn't set up
          any array of annotations (the ParameterAnnotationsAttribute is null).

          Later CtClassType interprets this as an empty array of annotations.

           static Object[][] toAnnotationType(ClassPool cp, ParameterAnnotationsAttribute a1,
           ParameterAnnotationsAttribute a2)
           throws ClassNotFoundException
           {
           int numParameters = 0;
           if (a1 != null)
           numParameters = a1.numParameters();
           else if (a2 != null)
           numParameters = a2.numParameters();
           else
           return new Object[0][]; // HERE
          


          Bug report: http://jira.jboss.com/jira/browse/JASSIST-19

          • 2. Re: rel 3.1 - Problem with CtBehavior#getParameterAnnotation
            chiba

            I have fixed this problem.