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 注释处理器?


- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01