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; } }
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 have solve this problem.