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?
To suppress violations you can use filter file. In this case you need to override default filter file. Spotbugs can't use default @SuppressWarnings annotation because it's a source annotation and not available in bytecode.
Use of @SuppressWarnings is to suppress or ignore warnings coming from the compiler, i.e., the compiler will ignore warnings if any for that piece of code. 1. @SuppressWarnings("unchecked") public class Calculator { } - Here, it will ignore all unchecked warnings coming from that class.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With