Java – how to suppress multiple findbugs warnings on the same line of code
I recently found findbugs @ edu umd. cs. findbugs. annotations. Suppresswarnings annotation, which is very cool and allows you to basically tell findbugs to ignore some warnings
I have successfully implemented my own slf4j binding, followed their suggestions, adopted slf4j simplicity, and modified it with my own recorder and recorder factory binding. I am glad to say that it works like a charm
I just found an error in the package containing this slf4j binding and complained about a 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 "is not final, but it should be." However, the (very) smart people on slf4j have thought of this and put around the comments provided above
Therefore, just to close findbugs, I modified the code to suppress FB warnings in my usual way:
@edu.umd.cs.findbugs.annotations.SuppressWarnings("MS_SHOULD_BE_FINAL") public static String REQUESTED_API_VERSION = "1.6";
When I clean up my project and rerun findbugs, I get a second warning on the same line of code. This time, I complain:
When I add a 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 got a compiler / syntax error from eclipse:
How to suppress multiple findbugs warnings on the same line of code?
Solution
Just list all warning identifiers in the array in a single comment:
@edu.umd.cs.findbugs.annotations.SuppressWarnings({ "MS_SHOULD_BE_FINAL","URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"}) public static String REQUESTED_API_VERSION = "1.6";
Like standard Java Like lang. suppresswarnings, the findbugs version is also has a parameter of type string [] For individual values, curly braces can be omitted to make life easier