[jboss-user] [Javassist user questions] - Re: adding annotations

cat4hire do-not-reply at jboss.com
Tue Apr 1 10:40:07 EDT 2008


After a lot I found that the following is the code that works for me:


  | 	ClassFile classFile = newProxyClass.getClassFile();
  | 	ConstPool constantPool = classFile.getConstPool();
  | 	AnnotationsAttribute attr = new AnnotationsAttribute(constantPool, AnnotationsAttribute.visibleTag);
  | 	javassist.bytecode.annotation.Annotation a = new javassist.bytecode.annotation.Annotation(this.annotationClassName, constantPool);
  | 	attr.setAnnotation(a);
  | 	classFile.addAttribute(attr);
  | 	classFile.setVersionToJava5();
  | 	
  | 	
  | 	ByteArrayOutputStream bos = new ByteArrayOutputStream();
  | 	DataOutputStream os = new DataOutputStream( bos );
  | 	classFile.write( os );
  | 	os.close();
  | 	
  | 	// all done
  | 	return bos.toByteArray();
  | 

the above produces the byte code for a class with the added annotation.

Thanks,
Luca

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

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



More information about the jboss-user mailing list