我使用javassist創建類並向其添加註釋。當我使用CtClass.writeFile並且看到帶有Java反編譯器的類文件時,註釋就在那裏,但是當我使用class.getAnnotations()或class.getDeclaredAnnotations()時,列表是空的。Javassist - 爲類添加註釋不起作用於反射
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(cl.loadClass("javax.jws.WebService")));
CtClass pikoClass = pool.makeClass("br.com.stuff.Piko");
ConstPool constPool = pikoClass.getClassFile().getConstPool();
AnnotationsAttribute attr = new AnnotationsAttribute(constPool, annotationsAttribute.visibleTag);
Annotation annoWebService = Annotation(constPool, pool.get("javax.jws.WebService"));
attr.setAnnotation(annoWebService);
pikoClass.getClassFile().addAttribute(attr);
Class piko = pikoClass.toClass();
piko.getDeclaredAnnotations(); // this is always empty
// Also tried
Annotation annoWebService = new Annotation("WebService", constPool);
Annotation annoWebService = new Annotation("@WebService", constPool);
Annotation annoWebService = new Annotation("javax.jwsWebService", constPool);
Annotation annoWebService = new Annotation("@javax.jwsWebService", constPool);
考慮紀念這一問題作爲解決進口;) – Stephan 2011-08-05 12:54:31