如何写一个Java注释处理器? [英] How to write a Java annotation processor?

查看:217
本文介绍了如何写一个Java注释处理器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能只是找错了方向,但我找到注释处理很...稀疏的JSE文件。我想写一个注解处理器,处理注释的字符串字段和局部变量与计算字符串前pression替代它们。这应该不是太复杂,但我pretty中的Javadoc javax.annotation.processing中丢失。

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.

编辑:我需要处理在编译时的注解,因为我想修改生成的code。就应更换注明字符串常量前pressions与计算字符串前pression。

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注释处理器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆