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

cat4hire do-not-reply at jboss.com
Mon Feb 25 12:09:22 EST 2008


I've tried to define the class within the class loader, introspecting it in place to see what happens. In other words, I added the following code:



  | 		    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();
  | 		    baseProxyClass = null;
  | 
  | 		    Class clazz = this.defineClass(subProxyClassName, bytecode, 0, bytecode.length);
  | 		    System.out.println("Class " + clazz.getName());
  | 		    System.out.println("Superclass " + clazz.getSuperclass());
  | 		    System.out.println("Annotations");
  | 		    try {
  | 			AgentProxy aProxy = (AgentProxy) clazz.newInstance();
  | 		    } catch (InstantiationException e) {
  | 			// TODO Auto-generated catch block
  | 			e.printStackTrace();
  | 		    } catch (IllegalAccessException e) {
  | 			// TODO Auto-generated catch block
  | 			e.printStackTrace();
  | 		    }
  | 		    for(Annotation a : clazz.getAnnotations())
  | 			System.out.println("annotazione " + a);
  | 

I believe that the way I'm using the ClassFile is wrong, since I'm not able to load the class, or better the class that is loaded is unable to be instantied as child of the parent class. 
The class hierarchy is the following:
proxy
  ^
   |
proxy_roled_1_24325842     <- when creating this class 
                                             I need to inject an annotation



  | Class whitecat.proxy_roled_1_24325842
  | Superclass class whitecat.proxy
  | Annotations
  | java.lang.InstantiationException: whitecat.proxy_roled_1_24325842
  | 	at java.lang.Class.newInstance0(Class.java:340)
  | 	at java.lang.Class.newInstance(Class.java:308)
  | 	at whitecat.core.RoleEngine.findClass(RoleEngine.java:701)
  | 	at whitecat.core.RoleEngine.injectAnnotation(RoleEngine.java:538)
  | 	at whitecat.example.ExampleMain.run(ExampleMain.java:132)
  | 	at java.lang.Thread.run(Thread.java:619)
  | 


Any suggestion about this?

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

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



More information about the jboss-user mailing list