Bad Practice

CNT: Rough value of known constant found

SpotBugs bug description

Example for bug:

        circumference = diameter * 3.141;

In this example the circumference of a circle is calculated. The value ̣π is is inserted as an approximate value. It is better to use a constant (e.g. Math.PI).

Project example: KnownConstant

NP: Method with Boolean return type returns explicit null

SpotBugs bug description

Example for bug:

    public Boolean booleanMethod() {
        return null;
    }

In this example a method with the return type Boolean returns null directly. This will be found by spotbugs and remarked as bug.

Project example: BooleanReturnsNull

This bug will not be found when a variable is defined which can return null.

BC: Equals method should not assume anything about the type of its argument

SpotBugs bug description

Example for bug:

    public boolean equals(Object o) {
        Sample casted = (Sample)o;
        ...
    }

In this example a ClassCastException would be thrown because the object o is cast without check. The equals(Object) method should never throw any exception as this violates the contract.

Project example: EqualsAssume

BIT: Check for sign of bitwise operation

SpotBugs bug description

Example for bug:

    private static final int FLAG = 0x40000000;
    public boolean bug(int value) {
        boolean flagSet = ((value & FLAG) > 0);
        ...
    }

In this example the check ((value & FLAG) > 0) will fail when FLAG is a negative value (e.g. 0x80000000). Then the result will be also negative.

Project example: CheckSign

See also

  • BIT: Check for sign of bitwise operation

CN: Class implements Cloneable but does not define or use clone method

SpotBugs bug description

Example for bug:

public class NoClone implements Cloneable {
    // no clone()
}

In this example the created class does not override the clone() method.

Project example: NoClone

CN: clone method does not call super.clone()

SpotBugs bug description

Example for bug:

    public Object clone() throws CloneNotSupportedException {
        // no call to super.clone();
    }

In this example the created class does call the clone() method of the super class. Every class that implements the Cloneable must call the method super.clone() to ensure that the clone() returns the correct class.

Project example: NoSuperClone

CN: Class defines clone() but doesn’t implement Cloneable

SpotBugs bug description

Example for bug:

public class NoClone {
    public Object clone() throws CloneNotSupportedException {
        ...
    }
}

In this example the created class overrides the method clone() but does not implement the interface Cloneable. This is at least uncommon and not expected.

Project example: NoCloneable

Co: Abstract class defines covariant compareTo() method

SpotBugs bug description

Example for bug:

public abstract class Covariant implements Comparable<Covariant> {
    @Override
    public abstract int compareTo(Covariant o);
}

In this example the class Covariant implements the interface Comparable and implements the method compareTo with itself as argument. This is problematic as subclasses cannot override this method with another argument.

Project example: Covariant