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

cat4hire do-not-reply at jboss.com
Wed Mar 12 07:52:19 EDT 2008


Hi,
I'm still fighting with the annotation problem. I've made a code clean up, and now I've got the following piece of code:


  | 		    // make a new class 
  | 		    CtClass newProxyClass = pool.makeClass( subProxyClassName );
  | 		    newProxyClass.setSuperclass( baseProxyClass );
  | 		    
  | 		    // place a default constructor in the new class
  | 		    CtConstructor constructor = new CtConstructor(null, newProxyClass );
  | 		    constructor.setBody(";");
  | 		    newProxyClass.addConstructor(constructor);
  | 
  | 		    // get the class file and add the annotation
  | 		    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();
  | 
  | 		    // transform the classfile into bytecode
  | 		    ByteArrayOutputStream bos = new ByteArrayOutputStream();
  | 		    DataOutputStream os = new DataOutputStream( bos );
  | 		    classFile.write( os );
  | 		    os.close();
  | 
  | 		    // load the class
  | 		    return this.defineClass(subProxyClassName, bytecode, 0, bytecode.length);
  | 

at runtime no exception are thrown, but the new class still has no annotation added!

Then I saved the bytecode to a file in two cases, the above piece of code and the one without the addAttribute, and I can see they are different, so it seems as the annotation is added, but it is not visible to the runtime system.

Please, any suggestion is appreciated!!!

Thanks,
Luca

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

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



More information about the jboss-user mailing list