Add more spotbugs checks

Add more thorough spotbugs checks

The Jenkins plugin pom enables static analysis with spotbugs. Plugin maintenance can be improved in some cases by increasing the depth of spotbugs analysis.

Create a branch

Create a git branch for your local work with the command:

$ git checkout -b add-spotbugs-checks master

Increase spotbugs checks

To increase the spotbugs analysis checks, add the spotbugs properties entries in the properties section of the pom.xml file:


Review and fix spotbugs warnings

When the spotbugs analysis checks are increased, they often report new issues that need to be resolved or suppressed. Spotbugs checks are included in the Apache Maven verify step. Run the spotbugs analysis checks as part of the Apache Maven verify step with the command:

$ mvn clean -DskipTests verify

It is generally preferred to fix a spotbugs warning rather than suppress the warning message. However, in those cases where a spotbugs message is incorrect or is infeasible to fix, it can be suppressed with the SuppressFBWarnings annotation. A suppression might look like this:

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/* ... preceding a method that returns a Boolean and may return null */
            value = "NP_BOOLEAN_RETURN_NULL",
            justification = "Null return indicates others should evaluate further")

Add a spotbugs exclusions file (if needed)

Sometimes the number of spotbugs exclusions make it inconenient or tedious to place the exclusions in the source files. In those cases, a spotbugs exclusions file can be used to list the spotbugs warnings that are being excluded and the classes, methods, and fields involved.

A good example of the spotbugs exclusions file and its configuration is available from Jenkins core. See the src/spotbugs/spotbugs-excludes.xml source file for examples. See the pom.xml file for the property that enables the spotbugs excludeFilterFile.

An example excludes filter file is also included here:

<?xml version="1.0" encoding="UTF-8"?>
    Exclusions in this section have been triaged and determined to be
    false positives.

    Here lies technical debt. Exclusions in this section have not yet
    been triaged. When working on this section, pick an exclusion to
    triage, then:

    - Add a @SuppressFBWarnings(value = "[...]", justification = "[...]")
      annotation if it is a false positive.  Indicate the reason why
      it is a false positive, then remove the exclusion from this

    - If it is not a false positive, fix the bug, then remove the
      exclusion from this section.
        <Class name="io.jenkins.plugin.example.ExampleAction"/>
        <Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING"/>
        <Class name="io.jenkins.plugin.example.SomeFeature"/>

Create a pull request

Commit that change:

$ git add pom.xml src/spotbugs
$ git commit -m "Increase spotbugs effort and threshold"

Push the change to GitHub:

$ git push origin --set-upstream add-spotbugs-checks
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Create a pull request for 'add-spotbugs-checks' on GitHub by visiting:
 * [new branch]      add-spotbugs-checks -> add-spotbugs-checks
Branch 'add-spotbugs-checks' tracking remote branch 'add-spotbugs-checks'.

Notice that the output of the command includes the URL, which can be used to open a pull request. Copy that URL in your web browser and submit a pull request.