SuppressWarnings not working on FindBugs

13,943

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

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

Share:
13,943
IAmYourFaja
Author by

IAmYourFaja

my father is a principal at burgoyne intnl and got me this job programming lisp and development. I aspire to unittesting with a concentration in mobile platforms.

Updated on June 04, 2022

Comments

  • IAmYourFaja
    IAmYourFaja almost 2 years

    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?!?