Embracing the Messiness in Search of Epic Solutions

Suppressing FindBugs Warnings

Posted

in

PROBLEM

FindBugs is one of the many great static code analysis tools that I use everyday. However, the generated report may usually contain a few false positives that forces me to weave through them whenever I rerun my build on Jenkins.

For example, I’m using Google Guava to construct my equals(...) and hashCode():-

public class Person {
    private String firstName;
    private String lastName;
    private Long age;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person other = (Person) o;

        return Objects.equal(firstName, other.firstName) &&
               Objects.equal(lastName, other.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(firstName, lastName);
    }

    // getters and setters
}

FindBugs will produce a EQ_UNUSUAL warning with the following description:-

myproject.Person.equals(Object) is unusual</code>
<code>
</code>
<code>This class doesn't do any of the patterns we recognize for checking
that the type of the argument is compatible with the type of the this
object. There might not be anything wrong with this code, but it is
worth reviewing.

SOLUTION

There are 2 known ways that I know to suppress these warnings. One way is to create FindBugs filter files, which I find very tedious. The other way is to use FindBug’s annotations to do so, which is what I’m going to show here.

First, we need to include the neccessary dependency:-

<dependency>
    <groupId>net.sourceforge.findbugs</groupId>
    <artifactId>annotations</artifactId>
    <version>1.3.2</version>
</dependency>

Next, we annotation equals(...) to suppress that specific warning:-

public class Person {
    private String firstName;
    private String lastName;
    private Long age;

    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "EQ_UNUSUAL",
                                                      justification="Implemented using Google Guava")
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person other = (Person) o;

        return Objects.equal(firstName, other.firstName) &amp;&amp;
               Objects.equal(lastName, other.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(firstName, lastName);
    }

    // getters and setters
}

That’s it… it’s that simple.

Yes, I know there’s this ugly FindBugs dependency in my code. However, I’ll take that any day so that I’m getting a cleaner report from FindBugs. I can also be absolutely sure that I have reviewed the generated warnings and decided that they are safe to be ignored.

Comments

Leave a Reply