JBoss Community

Re: How to add parameter annotation to new Method ?

created by jessiee white in Javassist - View the full discussion

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;
     parameterAtrribute.setAnnotations(paramArrays);



Reply to this message by going to Community

Start a new discussion in Javassist at Community