注释处理器可以用于代码生成吗? [英] Can annotation processor be used for code generation?

查看:23
本文介绍了注释处理器可以用于代码生成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我定义了一个名为 @MyAnnotation 的注释.

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

有一个类 X 声明为:

@MyAnnotation
class X { .... }

现在在编译时,我想检查所有用 @MyAnnotation 注释的类,并对需要在同一过程中编译的更多 java 源文件进行一些代码生成.

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.

这是否可以使用 java 注释处理器或其他工具?

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

推荐答案

你可以看看 Java apt (Annotation Processing Tool) 这样的事情.

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

您可以找到入门页面和一篇不错的文章(123) 关于如何使用它来生成代码.

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

这篇关于注释处理器可以用于代码生成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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