[jboss-user] [Javassist user questions] - Re: adding annotations
cat4hire
do-not-reply at jboss.com
Fri Feb 29 04:32:04 EST 2008
I've found how to make the ClassFile working for defining my new class, but I'm still unable to add the annotation. Now I work as follows:
1) I create a CtClass object and set the inheritance chain on that;
2) I get the byte[] from the CtClass and place them into a ByteArrayInputStream, used to create a ClassFile;
3) I add the annotation to the classfile;
4) I get the byte[] from the classfile thru a ByteArrayOutputStream;
5) I load the class thru the defineClass method.
Now the class is of the right type, but it has no the annotation.
The following is my code; anyone has an idea of which is my error adding the annotation?
| subProxyClassName += this.getSubClassNameSuffix();
|
| // make a new class and prepare the annotation
| CtClass newProxyClass = pool.makeClass(subProxyClassName);
| newProxyClass.setSuperclass( baseProxyClass );
| ByteArrayInputStream is = new ByteArrayInputStream( newProxyClass.toBytecode() );
| DataInputStream dis = new DataInputStream( is );
| ClassFile classFile = new ClassFile( dis );
| newProxyClass.defrost();
| ConstPool constantPool = classFile.getConstPool();
| AnnotationsAttribute attribute = new AnnotationsAttribute( constantPool, AnnotationsAttribute.visibleTag );
| javassist.bytecode.annotation.Annotation annotation = new javassist.bytecode.annotation.Annotation( this.annotationClassName, constantPool );
| attribute.setAnnotation( annotation );
| classFile.addAttribute( attribute );
| classFile.setVersionToJava5();
| ByteArrayOutputStream bos = new ByteArrayOutputStream();
| DataOutputStream os = new DataOutputStream( bos );
| classFile.write( os );
| new javassist.bytecode.annotation.Annotation( constantPool, subProxy );
| bytecode = bos.toByteArray();
| baseProxyClass = null;
|
| ...
|
| Class clazz = this.defineClass(subProxyClassName, bytecode, 0, bytecode.length);
|
|
Thanks,
Luca
View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4133098#4133098
Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4133098
More information about the jboss-user
mailing list