problem adding annotations at runtime
inalasuresh Feb 19, 2010 4:20 AMHi One & All
I have a classfile called HelloWorldService with @WebServiceClient annotation. I want to use this classfile and need to be add one more annotation @HandlerChain using javaassist.
Code Snipped which I am working on.
When I am invoking the below code snipped I am getting only one annotation it is removing all the annotations and adding the new one.
I am able to add annotation at runtime but it is adding only one annotation. But I want all the annotations? how to add multiple annotations?.
HelloWorldService.java
@WebServiceClient(name = "HelloWorldService")
public class HelloWorldService extends Service
{
}
Adding annotatins using javaassist
AddAnnotations.java
public byte[] makingAnnotationAtRuntime() throws Exception {
ClassPool pool = ClassPool.getDefault();
CtClass proxyClass = pool.get("HelloWorldService");
//get annotations from class called HelloWorldService
Object[] all = proxyClass.getAnnotations();
// get the class file and add the annotation
ClassFile classFile = proxyClass.getClassFile();
ConstPool constantPool = classFile.getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constantPool,AnnotationsAttribute.visibleTag);
Annotation annotation = new Annotation("javax.jws.HandlerChain", constantPool);
annotation.addMemberValue("file", new StringMemberValue("HelloWorldService.xml",constantPool));
attr.setAnnotation(annotation);
proxyClass.getClassFile().addAttribute(attr);
classFile.setVersionToJava5();
// transform the classfile into bytecode
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(bos);
classFile.write(os);
os.close();
//get annotations from Class
all = proxyClass.getAnnotations();
int i=0;
for (Object object : all) {
System.out.println(all[i]);
i++;
}
return bos.toByteArray();
}
When I run this I am getting the output : @javax.jws.HandlerChain(file="HelloWorldService_handler.xml")
But I want to get the other annotation as well. Original HelloWorldService Classfile contains @WebServiceClient(name = "HelloWorldService")
How can I get all annotations at run. Please suggest me I stuck to achieve this issue.
Please throw your thoughts.
Thanks in Advance
Suresh