How to write a Java annotation processor?(如何编写 Java 注释处理器?)
问题描述
我可能只是在寻找错误的方向,但我发现关于注释处理的 JSE 文档非常……稀疏.我想编写一个注释处理器来处理带注释的字符串字段和局部变量,以用计算的字符串表达式替换它们.这不应该太复杂,但我在 javax.annotation.processing 的 Javadoc 中迷失了.
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.
我需要在编译时处理注释,因为我想修改生成的代码.它应该用计算的字符串表达式替换带注释的常量字符串表达式.
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.
推荐答案
编译时注释处理器无法做到这一点.编译时注释处理器只能生成新文件(和类),它们不能修改现有类.您可以在运行时进行反射,但严格来说您不称为注释处理.此外,您将无法访问局部变量.
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.
如果您正在研究如何编写编译时注释处理器,请查看 https://github.com/pellaton/spring-configuration-validation-processor
If you're looking on how to write a compile time annotation processor check out https://github.com/pellaton/spring-configuration-validation-processor
这篇关于如何编写 Java 注释处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何编写 Java 注释处理器?


基础教程推荐
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01