1 Reply Latest reply on Oct 12, 2006 10:55 PM by coral

    add a new annotation problem

    coral

      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!!