SuppressWarnings 不适用于 FindBugs [英] SuppressWarnings not working on FindBugs

查看:40
本文介绍了SuppressWarnings 不适用于 FindBugs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 Eclipse 项目上运行 FindBugs 并收到一个潜在的错误警告,我想出于特定原因(在此问题的上下文之外)取消该警告.代码如下:

I ran FindBugs on my Eclipse project and got a potential bug warning that I would like to suppress for a specific reason (outside the context of this question). Here's the code:

public class LogItem {
    private String name;

    private void setName(final String nm) {
        name = nm;
    }
}

当你在这个类上运行 FindBugs 时,会在 name = nm 赋值操作上给你一个警告,说明:Unread field: com.me.myorg.LogItem.name.

When you run FindBugs on this class is gives you a warning on the name = nm assignment operation, stating: Unread field: com.me.myorg.LogItem.name.

所以我尝试添加:

    private void setName(final String nm) {
        @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP", justification = "Because I can")
        name = nm;
    }

当我这样做时,我在 Eclipse 中收到一个语法(编译)错误,说明:

When I do this I get a syntax (compile) error in Eclipse, stating:

复制局部变量nm;名称无法解析为类型.

Duplicate local variable nm; name cannot be resolved to a type.

然后我尝试在字段本身上添加 FindBugs 的 SuppressWarnings,但是在类上重新运行 FindBugs 后,FindBugs 仍然抱怨同一行代码并且出于同样的原因.我什至尝试将 SuppressWarnings 添加到 setName 方法中,但仍然没有区别.

So then I tried adding the FindBugs' SuppressWarnings on the field itself, but after re-running FindBugs on the class, FindBugs is still complaining about the same line of code and for the same reason. I even tried adding the SuppressWarnings to the setName method, and still no difference.

我如何(正是!)使用这个注释来让 FindBugs 安静下来?!?

How (exactly!) do I use this annotation to quiet FindBugs?!?

推荐答案

将注解放在字段上并修复错误标识符.这对我有用:

Put the annotation on the field and fix the bug identifier. This works for me:

public class LogItem {
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("URF_UNREAD_FIELD")
    private String name;

这篇关于SuppressWarnings 不适用于 FindBugs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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