我正在使用 javassist 创建一个类并为其添加注释。当我使用 CtClass.writeFile 并且看到带有 Java 反编译器的类文件时,注释就在那里,但是当我使用 class.getAnnotations() 或 class.getDeclaredAnnotations() 时,列表为空。
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);