Suppressing Java FindBugs Warnings
Sometimes, static code analyzers will find and report issues that are false positives, or may not be relevant. For example, the following code was creating a Findbug warning:
if (car.getMake() instanceof CarManufacturer) {
There is really nothing wrong with this code, but FindBugs was creating the following report:
BC: instanceof will always return true for all nonNull values in com.jalamor.cars.service.helper.CarHelper.getCarMake(car), since all com.jalamor.cars.service.helper.CarHelper are instances of com.jalamor.cars.service.helper.CarHelper
This instanceof test will always return true (unless the value being tested is null). Although this is safe, make sure it isn’t an indication of some misunderstanding or some other logic error. If you really want to test the value for being null, perhaps it would be clearer to do better to do a null test rather than an instanceof test.
To silence this warning (and other like it):
1. Get the
FindBugs.zip
and unzip it to a temp folder.
Find the
annotations.jar
and
jsr305.jar
and copy them to your project lib folder. If you are using Gradle, you could use these dependencies
compileOnly 'com.google.code.findbugs:annotations:3.0.1'
compileOnly 'com.google.code.findbugs:jsr305:3.0.1'
Add them to your project classpath (manually if not using Gradle or Maven) and by running the appropriate Gradle task if you are (e.g.
gradle cleanEclipse eclipse
) using Gradle.
Annotate the method that contains the FindBug (note it’s type value is ‘
BC
‘) warning you want to ignore. e.g. To ignore the warning on the code mentioned above we could add the following annotation above the method signature:
@SuppressFBWarnings(value = "BC", justification = "because we know better")
The import for the
@SuppressFBWarnings
class is
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy