java SuppressWarnings 不适用于 FindBugs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10884606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:58:35  来源:igfitidea点击:

SuppressWarnings not working on FindBugs

javaannotationsfindbugs

提问by IAmYourFaja

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:

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

public class LogItem {
    private String name;

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

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

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

So I tried adding this:

所以我尝试添加这个:

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

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

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

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

重复的局部变量 nm;名称无法解析为类型。

So then I tried adding the FindBugs' SuppressWarningson 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 SuppressWarningsto the setNamemethod, and still no difference.

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

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

我如何(确切地说!)使用此注释来使 FindBugs 安静?!?

采纳答案by TimK

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;

回答by Péter T?r?k

I have always used the built-in java.lang.SuppressWarnings, not that of FindBugs, and it worked so far. Also, for specific statements you may need to keep the statement on the same line right after the annotation. Like

我一直使用内置的java.lang.SuppressWarnings,而不是 FindBugs 的,并且到目前为止它有效。此外,对于特定的语句,您可能需要将语句放在注释之后的同一行上。喜欢

    @SuppressWarnings("NP") name = nm;

Also, are you sure "NP"is a valid warning identifier here? I would try "unused"if nothing else seems to work.

另外,你"NP"确定这里是一个有效的警告标识符吗?"unused"如果没有其他任何效果,我会尝试。