SuppressWarnings not working on FindBugs

陌路散爱 提交于 2019-12-07 11:11:03

问题


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;
    }
}

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.

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:

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

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.

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


回答1:


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;



回答2:


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

    @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.



来源:https://stackoverflow.com/questions/10884606/suppresswarnings-not-working-on-findbugs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!