从 AnnotationProcessor 读取字节码
可能的重复:
插入 Java 编译器
编辑 - 这似乎是一个插入Java编译器的骗局
我想实现一个< a href="http://java.sun.com/j2se/1.5.0/docs/guide/apt/mirror/com/sun/mirror/apt/AnnotationProcessor.html" rel="nofollow noreferrer">AnnotationProcessor
与 apt< 一起使用/a>
将类编译为字节码后将调用的工具,可以读取和修改字节码。
这样做的原因是我想将带注释的方法翻译为另一种语言,并用调用翻译版本的存根替换 java 方法。
但是 AnnotationProcessorEnvironment
接口只提供生成新类的方法,不提供读回上一轮生成的类文件的方法。
检测 API 执行的操作与我想要的类似,但仅在运行时执行。我正在寻找一种在编译时执行此操作的方法。
Possible Duplicate:
Plugging in to Java compilers
Edit - this appears to be a dupe of Plugging in to Java compilers
I would like to implement an AnnotationProcessor
for use with the apt
tool that will be invoked after compiling a class to bytecode, that can read and modify the bytecode.
The reason for doing this is that I want to translate annotated methods to another language and replace the java methods with stubs that invoke the translated versions.
However the AnnotationProcessorEnvironment
interface only provides methods to generate new classes, not to read back a class file that was generated in a previous round.
The instrumentation API does something similar to what I want, but only at run-time. I am looking for a way to do this at compile time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我想做一些操作时我看了看在编译器中,但最终使用了后处理器。
您可以使用 APT 操作抽象语法树 (AST),但只能使用特定于编译器的 hack。如果您想要了解如何完成此操作的示例,Project Lombok 使用 Sun
javac
和 Eclipse 编译器。目前看来还没有更好的方法。I had a look when I wanted to do some manipulation in the compiler, but ended up using a post-processor.
You can manipulate the abstract syntax tree (AST) using the APT, but only with compiler-specific hacks. If you want a sample of how that's done, Project Lombok does it with the Sun
javac
and Eclipse compilers. At present, there doesn't seem to be a better method.