Can annotation processor be used for code generation?

限于喜欢 提交于 2020-01-03 07:21:09

问题


Let's say I define an annotation called @MyAnnotation.

There is a class X which is declared as:

@MyAnnotation
class X { .... }

Now at compile time I want to inspect all classes annotated with @MyAnnotation and do some code generation to more java source files that need to be compiled as well in the same process.

Is this possible using java annotation processor or some other tool?


回答1:


You may take a look at the Java apt (Annotation Processing Tool) for such a thing.

You can find the Getting Started page, and a nice article (1, 2, 3) about how to use that to generate code.




回答2:


APT tool has been merged into javac in Java 6. This is a much better tutorial for annotation processing.



来源:https://stackoverflow.com/questions/14302418/can-annotation-processor-be-used-for-code-generation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!