[jboss-user] [Javassist user questions] - Re: Adding annotations to methods

PaulKeeble do-not-reply at jboss.com
Sat Jul 4 22:02:19 EDT 2009


In the end I managed to work it out and answer my own question. The key was understanding that you need to add the AnnotationsAttribute instance to the Info of the method.

That is the rough solution is:


  | CtClass clazz = ....
  | ClassFile classFile = clazz.getClassFile();
  | ConstPool cp = classFile.getConstPool();
  | AnnotationsAttribute attr = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); 
  | 
  | Annotation useAnnotation = new Annotation("org.package.AnnotationType",cp);
  | annotation.addMemberValue("name", new StringMemberValue("Paul",cp));
  | 
  | attr.setAnnotation(useAnnotation);
  | 
  | m.getMethodInfo().addAttribute(attr);
  | 
  | 

So the only real difference between the CtClass, CtMethod and even parameters is I needed to do the appropriate getXXXInfo and then add the attribute directly.

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4241819#4241819

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4241819



More information about the jboss-user mailing list