java 如何编写Java注解处理器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11385628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a Java annotation processor?
提问by Christian Schlichtherle
I may be just looking in the wrong direction but I find the JSE documentation on annotation processing very ... sparse. I want to write an annotation processor which processes annotated String fields and local variables to substitute them with a computed String expression. This should not be too complicated but I'm pretty lost in the Javadoc for javax.annotation.processing.
我可能只是看错了方向,但我发现关于注释处理的 JSE 文档非常......稀疏。我想编写一个注释处理器,它处理带注释的字符串字段和局部变量,用计算出的字符串表达式替换它们。这应该不会太复杂,但我在 javax.annotation.processing 的 Javadoc 中很迷茫。
EDIT: I need to process annotations at compile time because I want to modify the generated code. It should replace annotated constant String expressions with a computed String expression.
编辑:我需要在编译时处理注释,因为我想修改生成的代码。它应该用计算的字符串表达式替换带注释的常量字符串表达式。
采纳答案by Philippe Marschall
This can not be done with a compile time annotation processor. Compile time time annotation processors can only generate new files (and classes) they can not modify existing classes. You can do reflection at runtime but strictly speaking you that is not called annotation processing. Also you won't have access to local variables.
这不能用编译时注释处理器来完成。编译时注释处理器只能生成新文件(和类),它们不能修改现有类。您可以在运行时进行反射,但严格来说,这不称为注释处理。此外,您将无法访问局部变量。
If you're looking on how to write a compile time annotation processor check out https://github.com/pellaton/spring-configuration-validation-processor
如果您正在研究如何编写编译时注释处理器,请查看https://github.com/pellaton/spring-configuration-validation-processor
回答by mernst
Two tools that do this are Project Lombokand DuctileJ. Both of these tools existed at the time the question was originally asked; additional tools now surely exist.
执行此操作的两个工具是Project Lombok和DuctileJ。在最初提出问题时,这两种工具都存在;现在肯定存在其他工具。
The key idea is to write an annotation processor that traverses and modifies the program's AST (abstract syntax tree) during compilation, before code generation. The compiler won't change the source code on disk, but the generated .class file will reflect the changes that your annotation processor makes.
关键思想是编写一个注释处理器,在代码生成之前,在编译期间遍历和修改程序的 AST(抽象语法树)。编译器不会更改磁盘上的源代码,但生成的 .class 文件将反映注释处理器所做的更改。
You may be able to adapt one of these tools to suit your needs, or you could implement your own tool inspired by their implementation techniques.
您可以调整其中一种工具来满足您的需求,或者您可以根据他们的实施技术来实施您自己的工具。
Compile-time processing has two advantages over class-file processing. One is that the compiler usually has more information than is available from compiled code. Another is that everything happens in one step, during compilation, rather than requiring the developer to run a separate tool to rewrite the .class files after compilation.
与类文件处理相比,编译时处理有两个优点。一是编译器通常拥有比编译代码更多的信息。另一个是,一切都在一个步骤中发生,在编译期间,而不是要求开发人员在编译后运行单独的工具来重写 .class 文件。
回答by Ondra ?i?ka
Check
查看
- Javassist http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/
- ASM http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/
- Byteman (for runtime) http://www.jboss.org/byteman/