如何访问 RetentionPolicy.CLASS java 注释? [英] How can I access a RetentionPolicy.CLASS java annotation?

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

问题描述

有没有办法配置 ClassLoader 或 JVM 来加载带有 CLASS 保留策略的注释,以便我可以使用反射访问它们?

Is there a way to configure a ClassLoader or a JVM to load annotations with CLASS retention policy, so I can access them using reflection?

这对于编译时后处理很有用,正如前面提到的 这里.我注释了我的一些类以便自动生成一个 antlib.xml 文件.我希望我的注释可以有 CLASS 保留策略,这样它就不会创建运行时依赖项.

This is useful for compile-time post-processing, as mentioned here. I annotate some of my classes in order to generate an antlib.xml file automatically. I would prefer if my annotation could have CLASS retention policy, so that it does not create runtime dependencies.

推荐答案

javac 可以使用 -processor 选项处理源代码和类级别的注释.请参见 javax.annotation.processing.AbstractProcessor.(从 java 1.6 开始).

javac can process source and class level annotations, with the -processor option. See javax.annotation.processing.AbstractProcessor. (Since java 1.6).

我在编译 .java 文件时开始使用它.显然它也可以用于处理带有 .class 输入文件的 CLASS 注释.这个我没试过,因为我是用ant编译的,ant好像没有把.class文件传给编译器.

I started using it while compiling .java files. Apparently it can also be used to process CLASS annotations with .class input files. I haven't tried this because I'm using ant to compile, and ant does not seem to pass .class files to the compiler.

当我想处理项目中的所有注释时,我必须进行完整的编译.

I have to do a full compile when I want to process all the annotations in my project.

这篇关于如何访问 RetentionPolicy.CLASS java 注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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