我可以制作一个“扩展"的 Java 注释吗?@SuppressWarnings? [英] Can I make a Java Annotation which "extends" @SuppressWarnings?

查看:29
本文介绍了我可以制作一个“扩展"的 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.

这篇关于我可以制作一个“扩展"的 Java 注释吗?@SuppressWarnings?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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