問題描述
我可能只是在尋找錯誤的方向,但我發(fā)現(xiàn)關(guān)于注釋處理的 JSE 文檔非常……稀疏.我想編寫一個注釋處理器來處理帶注釋的字符串字段和局部變量,以用計算的字符串表達式替換它們.這不應(yīng)該太復雜,但我在 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.
我需要在編譯時處理注釋,因為我想修改生成的代碼.它應(yīng)該用計算的字符串表達式替換帶注釋的常量字符串表達式.
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.
推薦答案
編譯時注釋處理器無法做到這一點.編譯時注釋處理器只能生成新文件(和類),它們不能修改現(xiàn)有類.您可以在運行時進行反射,但嚴格來說您不稱為注釋處理.此外,您將無法訪問局部變量.
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
這篇關(guān)于如何編寫 Java 注釋處理器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!