如何编写 Java 注释处理器?

时间:2023-04-05
本文介绍了如何编写 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 注释处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:我应该将@Transactional 注释放在哪里:在接口定义或实现类中? 下一篇:Spring:在属性文件中定义 @RequestMapping 值

相关文章