如何编写 Java 注释处理器?

How to write a Java annotation processor?(如何编写 Java 注释处理器?)
本文介绍了如何编写 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 注释处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)