我可以在Java注释中添加“扩展"吗? @SuppressWarnings?

问题描述:

我知道无法扩展Java注释.

我为私有字段创建了一个注释,这意味着该字段很可能在声明它的类中未使用.因此,我在带注释的字段上收到很多未使用的字段"警告.

I've created an annotation for a private field which means that the field is likely to appear unused in the class in which it is declared. For this reason, I'm getting a lot of "unused field" warnings on my annotated fields.

有没有办法给我的注释赋予@SuppressWarnings("unused")的行为,所以我不必对每个具有@MyAnnotation的字段进行双重注释?

Is there any way to give my annotation the behaviour of @SuppressWarnings("unused") so I don't have to doubly-annotate every field which has @MyAnnotation?

快速答案是否". Java编译器对您的注释一无所知,因此不会按照您希望的方式对其进行处理.

The quick answer is "no". Java compiler doesn't know anything about your annotation, so it won't process it the way you want.

但是诚实的答案是是".在此文章中,您可以找到有关如何编写编译器插件的详细说明.您可以编写插件,如果找到注释,它将进行处理,并且不会将字段传递给未使用的检查器.

But the honest answer is "yes". In this article you can find detailed description of how to write compiler plugin. Chances are you can write plugin, which, in case of finding your annotation, will handle it and won't pass field to unused checker.