-
1. Re: How to add parameter annotation to new Method ?
jesbo Dec 4, 2011 6:16 AM (in response to fury)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);