抑制 "unused" 仍然在 Intellij 中发出警告?

Suppress "unused" still gives warning in Intellij?

我们有这样的代码:

class GetXyzResponseBean {
    @SuppressWarnings("unused")
    private final List<Foo> foos;
    public GetXyzResponseBean(List<Foo> foos) { this.foos = ...
} 

基本上是一个我们与 GSON 结合使用的简单 bean class。因此,我们的代码的某些部分创建了此 class 的实例(传入该列表),然后 JAX RS / gson magic 将其转换为 JSON 以供某些 REST 客户端使用。

事情是:虽然上面使用了@SuppressWarnings ...我仍然在 Intellij 中收到警告,建议我应该通过将它变成局部变量来修复它。

不是我想要的。我想保持代码原样,不要被这里的警告所困扰!我也不想为此字段添加 getter 方法。因为本意是这是一个"write only"容器。

Suppress the warning inside of IntelliJ 而不是在任何其他级别抑制它。

导航到编辑器 > 检查 > Java > Class 结构,然后取消选中 "Field can be local"。

您可能正在寻找抑制警告:

@SuppressWarnings ("FieldCanBeLocal")

因为,在你的情况下它没有被使用,你可能想使用以下内容:

@SuppressWarnings ({"FieldCanBeLocal", "unused"})

这是一份(非详尽无遗?)抑制警告列表:https://gist.github.com/elevenetc/bf795f94aaf3e92169ef