Tuesday, August 2, 2016

Suppress FindBugs Warnings in a Java and Spring Boot Web Application using Gradle

How to Suppress FindBugs Warnings using Annotations in a +Spring Boot and +Java Application



If your build is breaking because of a FindBugs issue and it is a false-positive or you are unable to resolve the issue because of other considerations, you can add an Annotation to ignore the Findbugs warning.


Update your Gradle Dependencies


You will want to add the following compile time dependency to your build.gradle file.

compile group: 'findbugs', name: 'findbugs', version: '1.0.0'

dependencies {

    compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}


Get the Findbugs Issue ID

You will need a specific ALL_CAPS identifier so that FindBugs knows what bug to ignore.


Locate the FindBugs Report


In your build message, you will see a link to the findbugs report:



You can also find the report in your build artifact.  Right-click and open in your preferred browser to view file.
find bugs html report in build artifact
Open the findbugs html report from the build directory


Open the FindBugs Report


Once you have it, open the Findbugs HTML report in a browser.  It should look like the following.

findbugs html report for java issues
Find Bugs HTML Report

Get the FindBugs identifier


Copy and paste the FindBugs identifier.  It should be in ALL_CAPS.

You will use the annotation like this:
@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")

Add the Annotation to Your Code


Be sure that the correct findbugs SuppressWarnings annotation is being used.

import edu.umd.cs.findbugs.annotations.SuppressWarnings;


Add Suppress warnings annotation


Add this Suppress warnings annotation above the offending line of code.  There are multiple versions of this annotation.

Be sure to use the specific FindBugs issue ID as a parameter in the annotation.


Be sure to use the annotation specific to findbugs in the package edu.umd.cs.findbugs.annotations.

Suppress warnings annotation
Use the edu.umd.cs.findbugs.annotation version of @SuppressWarnings


This is the annotation you will want to add above the offending line of code.

@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")



Re-run the Build to Verify that the Warning is being Suppressed


You want to ensure that the warning is being ignored correctly.  You may want to add a TODO as well if you intend to fix the issue at a later point as well or at least track the issues you are suppressing in some way.

Your build should run successfully without issuesmoving forward now.



Further info: