Exploring the FindBugs plugin

This section describes the configuration and usage of the FindBugs plugin.

FindBugs works with three types of errors. You can visit http://findbugs.sourceforge.net/bugDescriptions.html for the FindBugs error details. The following are the FindBugs-supported error categories and errors:

  • Correctness bug: This is an apparent coding mistake that results in code that was probably not what the developer intended; for example, a method ignores the return value of a self-assigned field. The following are a few examples of a correctness bug:
    • The class defines tostring() but it should be toString()
    • A value is checked here to see whether it is null, but this value can't be null because it was previously dereferenced, and if it were null, a null pointer exception would have occurred at the earlier dereference
    • The method in the subclass doesn't override a similar method in a superclass because the type of a parameter doesn't exactly match the type of the corresponding parameter in the superclass
    • Class defines equal(Object) but it should be equals(Object)
  • Bad practice: This includes violations of recommended best practices and essential coding practice. The following are the examples of bad practices:
    • Hash code and equals problems:
      • Class defines hashCode() but it should be equals() and hashCode()
      • Class defines equals() but it should be hashCode()
      • Class defines hashCode() and uses Object.equals()
      • Class defines equals() and uses Object.hashCode()
    • Cloneable idiom:
      • Class defines clone() but doesn't implement Cloneable
    • Serializable problems:
      • Class is Serializable, but doesn't define serialVersionUID
      • Comparator doesn't implement Serializable
      • Non serializable class has a serializable inner class
    • Dropped exceptions: Here, an exception is created and dropped rather than thrown, such as the following example, where the exception was created but not thrown:
      if (x < 0)
        new IllegalArgumentException("x must be nonnegative");
    • Misuse of finalize:
      • Explicit invocation of finalize
      • Finalizer does not call the superclass finalizer
  • Dodgy errors: This kind of code is confusing, anomalous, or written in a way that leads to errors. Examples include the following:
    • Dead store of class literal: An instruction assigns a class literal to a variable and then never uses it.
    • Switch fall through: A value stored in the previous switch case is overwritten here due to a switch fall through. It is likely that you forgot to put a break or return at the end of the previous case.
    • Unconfirmed type casts and redundant null check: This error occurs when a value is null, for example, consider the following code:
      Object x = null;
      Car myCar = (Car)x;
      if(myCar != null){
        //...
      }

The following is the update site URL for the FindBugs Eclipse plugin: http://findbugs.cs.umd.edu/eclipse.

You can also install it through Eclipse Marketplace.

Install FindBugs and then add the following code to the CodeQualityChapter06 project for verification:

public class Buggy implements Cloneable {
    private Integer magicNumber;
    public Buggy(Integer magicNumber) {
        this.magicNumber = magicNumber;
    }
    public boolean isBuggy(String x) {
        return "Buggy" == x;
    }
    public boolean equals(Object o) {
        if (o instanceof Buggy) {
            return ((Buggy) o).magicNumber == magicNumber;
        }
        if (o instanceof Integer) {
            return magicNumber == ((Integer) o);
        }
        return false;
    }
    Buggy() { }
    static class MoreBuggy extends Buggy {
        static MoreBuggy singleton = new MoreBuggy();
    }
    static MoreBuggy foo = MoreBuggy.singleton;
}

Right-click on the project and click on the Find Bugs menu. The following is the pop-up menu displayed:

Exploring the FindBugs plugin

Open the source file; it shows the bug icons. The following screenshot displays the bugs:

Exploring the FindBugs plugin

The following screenshot displays the bugs in a tabular format with the error categories:

Exploring the FindBugs plugin
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset