Javassist - 向类添加注释不适用于反射

发布于 2024-11-07 16:25:05 字数 1086 浏览 1 评论 0原文

我正在使用 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);

I'm creating a class using javassist and add annotation to it. When I use CtClass.writeFile and I see class file with Java decompiler the annotation is there but when I use class.getAnnotations() or class.getDeclaredAnnotations() the list is empty.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-11-14 16:25:05

问题解决了,我使用的是 3.1 版本,现在我使用的是 3.12.1.GA(最后在 maven 存储库上)并且在此版本上注释有效。

Problem solved, I was using version 3.1, now I'm using 3.12.1.GA (last on maven repository) and on this version annotations works.

一个人的旅程 2024-11-14 16:25:05

也许我很愚蠢,这是一个无用的答案,但如果你收到一个错误,说

注释是抽象的;无法实例化

请记住检查导入并确保导入正确的注释:

导入javassist.bytecode.annotation.Annotation;

并不是 Eclipse 自动导入的错误库让我浪费了二十分钟的时间 (java.lang.annotation.Annotation)

Maybe I am dumb and this is an useless answer but if you are receiving an errors that says

Annotation is abstract; cannot be instantiated

Remember to check the import and make sure that you are importing the right Annotation:

import javassist.bytecode.annotation.Annotation;

And not the wrong library automatically imported by Eclipse that made me waste twenty minutes of my life (java.lang.annotation.Annotation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文