1 Reply Latest reply on Dec 4, 2011 6:16 AM by jesbo

    How to add parameter annotation to new Method ?




      I faced with one case. On my work I got necessity make proxy for class which contain methods with annotatuions. Some methods has parameter annotations, like this:


      public void theGameIsRunning(@Named("test")String test){}


      But I can't find the way to do it with javassist.


      Probably someone know how to solve that ?


      Thanks, Denis.

        • 1. Re: How to add parameter annotation to new Method ?

          if you want to add annotion to argo,you have to add @parameter(name = "arg0") to the parameter.

             String addAnnotiontion(@Pparameter(name = "arg0")  String name,String card);


          use javassist to add annotion .the main train  like the below:


                * get constpool
               AttributeInfo paramAtrributeInfo = methodInfo.getAttribute(ParameterAnnotationsAttribute.visibleTag); // or inVisibleTag
               ConstPool parameterConstPool = paramAtrributeInfo.getConstPool();

                * param annotation
               Annotation parameterAnnotation = new Annotation("annotation name", parameterConstPool);
               ClassMemberValue parameterMemberValue = new ClassMemberValue("class full name", parameterConstPool);
               parameterAnnotation.addMemberValue("value", parameterMemberValue);

                * add annotation to dimensional array
               ParameterAnnotationsAttribute parameterAtrribute = ((ParameterAnnotationsAttribute) paramAtrributeInfo);
               Annotation[][] paramArrays = parameterAtrribute.getAnnotations();

               int orderNum = position.getOrderNumber();
               Annotation[] addAnno = paramArrays[orderNum];
               Annotation[] newAnno = null;
               if (addAnno.length == 0) {
                newAnno = new Annotation[1];
               } else {
                newAnno = Arrays.copyOf(addAnno, addAnno.length + 1);
               newAnno[addAnno.length] = parameterAnnotation;

               paramArrays[orderNum] = newAnno;