1 Reply Latest reply on Feb 19, 2010 8:19 AM by suresh inala

    problem adding annotations at runtime

    suresh inala Newbie

      Hi 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?.




      @WebServiceClient(name = "HelloWorldService")

      public class HelloWorldService extends Service




      Adding annotatins using javaassist




      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));




                  // transform the classfile into bytecode

                  ByteArrayOutputStream bos = new ByteArrayOutputStream();

                  DataOutputStream os = new DataOutputStream(bos);




                 //get annotations from Class

                  all = proxyClass.getAnnotations();

                  int i=0;

                  for (Object object : all) {





                  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


        • 1. Re: problem adding annotations at runtime
          suresh inala Newbie

          Hi Guys the problem is solved,

          the mistake was I’m creating a new instance of AnnotationsAttribute and need to use attr.addAnnotation(annotation); instead of attr.setAnnotation(annotation);


          Below code is for getting the annotations and add new annotations.


          Changes on Code snipped :


          AnnotationsAttribute attr = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);

          Annotation annotation = new Annotation("javax.jws.HandlerChain",constantPool);

          if (attr != null) {

                          annotation.addMemberValue("file", new StringMemberValue("HelloWorldClient_handler.xml", constantPool));







          i Succeed, with the above code,


          Thanks & Regards