How to suppress multiple FindBugs warnings for the same line of code

I recently discovered FindBugs' @edu.umd.cs.findbugs.annotations.SuppressWarnings annotation which is pretty cool and allows you to basically tell FindBugs to ignore certain warnings.

I've successfully implemented my own SLF4J binding by following their recommendations to take slf4j-simple and modify it with your own logger and logger factory bindings, and I'm pleased to say it works like a charm.

I just ran find bugs on the package that contains this SLF4J binding and it is complaining about a certain line of code written by the original StaticLoggerBinder author (Ceki Gulku):

// to avoid constant folding by the compiler, this field must *not* be final.
publicstatic String REQUESTED_API_VERSION = "1.6"; // !final

FindBugs complains that this field "isn't final but it should be". However, the (very) smart people over at SLF4J already thought of that, and placed the surrounding comments provided above.

So, just to get FindBugs to shut up, I've modified the code per my usual way of suppressing FB warnings:

@edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
public static String REQUESTED_API_VERSION = "1.6";

When I clean my project and re-run FindBugs, I get a second warning on the same line of code, this time complaining:

This field is never read. The field is public or protected, so perhaps it is intended to be used with classes not seen as part of the analysis. If not, consider removing it from the class.

When I add this second warning suppression:

@edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL")
@edu.umd.cs.findbugs.annotations.SuppressWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
public static String REQUESTED_API_VERSION = "1.6";

I get a compiler/syntax error from Eclipse:

Duplicate annotation @SuppressWarnings.

How can I suppress multiple FindBugs warnings on the same line of code?

Answers


Just list all the warning identifiers in an array, within a single annotation:

@edu.umd.cs.findbugs.annotations.SuppressWarnings({
        "MS_SHOULD_BE_FINAL", 
        "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public static String REQUESTED_API_VERSION = "1.6";

Just as for the standard java.lang.SuppressWarnings, the FindBugs version also has a parameter of type String[]. For a single value, the curly braces can be omitted though to make life easier.


FindBugs 3+ style:

  @SuppressFBWarnings(
    value={"NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE","STCAL_INVOKE_ON_STATIC_DATE_FORMAT_INSTANCE"},
    justification="let me just make the build pass")

Try this:

@edu.umd.cs.findbugs.annotations.SuppressWarnings({"MS_SHOULD_BE_FINAL" , "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"})
public static String REQUESTED_API_VERSION = "1.6";

Need Your Help

Why do I receive a DMARC report everyday?

xml email gmail dmarc

I've setup DMARC policy on my domain.

Using angular 2 material icons

angular angular-material

I'm trying to use angular material 2 to display icons on my website, but I'm a little confused.