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

cat4hire do-not-reply at jboss.com
Mon Feb 25 08:08:46 EST 2008


This is what I've tried to do in my class loader:


  | 		    subProxyClassName += this.getSubClassNameSuffix();
  | 		    
  | 		    // make a new class and prepare the annotation
  | 		    ClassFile classFile = new ClassFile( false, subProxyClassName, baseProxyClass.getName() );
  | 		    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();
  | 		    classFile.setSuperclass( baseProxyClass.getName() );
  | 		    ByteArrayOutputStream bos = new ByteArrayOutputStream();
  | 		    DataOutputStream os = new DataOutputStream( bos );
  | 		    classFile.write( os );
  | 		    bytecode = bos.toByteArray();
  |                     return this.defineClass(subProxyClassName, bytecode, 0, bytecode.length);
  | 

The above is the findClass method of my class loader. The idea is that I have to create a subclass of the baseProxyClass (a CtClass object) and I have to add to it the annotation indicated by annotationClassName. When I return from the findClass method, the Class object I've got says me that it is a subclass of my proxy, but it cannot be instantiated as a proxy object (InstantiationError), as well as the getAnnotations() method returns an empty array.
What am I doing wrong?

Thanks,
Luca

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

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



More information about the jboss-user mailing list