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

查看:71
本文介绍了如何访问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?

这对于编译时后处理很有用,如

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天全站免登陆