[jboss-dev-forums] [Javassist development Forum] - add a new annotation problem

coral do-not-reply at jboss.com
Thu Oct 12 01:14:37 EDT 2006


Hi all,
 I am working on ejb3 project. Ejb3 always use some annotations such as "@Stateless","@Remote", "@RemoteBinding" and etc. Now I want to use javassist to generate these annotations. But have some problem. Can you help me to point out my mistake?
<The class I wanna modify>

  | @Stateless
  | public class StatelessTest {
  |      public String sayHello(String user) {
  |          return "Hello" + user;
  |      }
  | 
  | }
  | 

<In this test case, I wanna put a new annotation "@RemoteBinding(jndiBinding = "StatelessTest /remote")" into StatelessTest class>


  | public class AnnotationTest extends TestCase {
  |    public void testAnnotation() {
  | ClassPool pool = ClassPool.getDefault();
  | pool.insertClassPath
  | ("D:/workspaces/.maven/repository/jboss/jars/jboss-annotations-ejb3-4.0.3.jar");
  | CtClass c = pool.get
  | ("com.quartet.fwk.container.hivemind.qar.models.StatelessModel");
  | ConstPool consts = c.getClassFile().getConstPool();
  | ByteArrayOutputStream output = new ByteArrayOutputStream();
  | AnnotationsWriter writer = new AnnotationsWriter(output, consts);
  | writer.numAnnotations(1);
  | writer.annotation("RemoteBinding", 1);
  | writer.memberValuePair("RemoteBinding");
  | writer.constValueIndex("remote");
  | writer.close();
  | byte[] attribute_info = output.toByteArray();
  | AnnotationsAttribute attribute=new AnnotationsAttribute(consts,
  |      AnnotationsAttribute.invisibleTag, attribute_info);
  | c.getClassFile().addAttribute(attribute);
  | Object[] objs = c.getAnnotations();
  | System.out.println(objs[1]);
  | 
I think these codes should work very well, but it throws exception:
java.lang.reflect.InvocationTargetException
detailMessage="bad descriptor: RemoteBinding";

thank for any replay!!

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

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



More information about the jboss-dev-forums mailing list