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

4

22

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?

Patriarchate answered 5/6, 2012 at 11:24 Comment(1)
Suggestion: Use SuppressFBWarnings so you can import it as you would any other class instead of using the fully-qualified name.Garratt
F
25

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.

Formalism answered 5/6, 2012 at 11:27 Comment(0)
K
15

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")
Kory answered 12/12, 2017 at 22:36 Comment(1)
justification="let me just make the build pass" I like this :)Benildis
D
1

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";
Dalesman answered 5/6, 2012 at 11:29 Comment(0)
Q
1

In case you got here in search of a solution for Kotlin: There the curly braces do not work. The compiler will complain with

Unexpected tokens (use ';' to separate expressions on the same line)

And what actually does work is:

@SuppressWarnings("MS_SHOULD_BE_FINAL", "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
Quite answered 4/10, 2023 at 21:12 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.