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).
circumference = diameter * 3.141;
This bug is only found when a certain threshold is exceeded (see the not detected example).
This warning can be suppressed with @SuppressFBWarnings(value="CNT_ROUGH_CONSTANT_VALUE").
In this example a method with the return type Boolean returns null directly. This will be found by spotbugs and marked as bug.
public Boolean booleanMethod() { return null; }
This bug will not be found when a variable is defined which can return null.
public Boolean variableBooleanMethod() { Boolean result = null; return result; }
This warning can be suppressed with @SuppressFBWarnings(value="NP_BOOLEAN_RETURN_NULL").
In this example the method setVisible() is called from the main thread which can cause deadlocks or calling methods on partially created Swing components.
public static void main(String... args) { JFrame jFrame = new JFrame(); jFrame.setVisible(true); }
This bug is only found when the problematic code is called in the main thread (directly from the main() method).
To fix the bug ensure that the problematic methods are called from the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() { @Override public void run() { jFrame.setVisible(true); } });
This warning can be suppressed with @SuppressFBWarnings(value="SW_SWING_METHODS_INVOKED_IN_SWING_THREAD").
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.
public boolean equals(Object obj) { EqualsAssume toCompare = (EqualsAssume)obj; ... }
This warning can be suppressed with @SuppressFBWarnings(value="BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS").
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 and the check will fail.
private static final int FLAG = 0x40000000; public boolean bug(int value) { boolean flagSet = ((value & FLAG) > 0); ... }
This bug will not be found when the FLAG is already negative (the highest significant bit is set).
private static final int HIGHEST_FLAG = 0x80000000; public boolean highestBitBug(int value) { boolean flagSet = ((value & HIGHEST_FLAG) > 0); ... }
In this example the created class does not override the clone() method.
public class NoClone implements Cloneable { // no clone() }
In this example the created class does not 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.
public Object clone() throws CloneNotSupportedException { // no call to super.clone(); }
This warning can be suppressed with @SuppressFBWarnings(value="CN_IDIOM_NO_SUPER_CALL").
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.
public class NoCloneable { public Object clone() throws CloneNotSupportedException { ... } }
This warning can be suppressed with @SuppressFBWarnings(value="CN_IMPLEMENTS_CLONE_BUT_NOT_CLONEABLE").
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.
public abstract class Covariant implements Comparable<Covariant> { @Override public abstract int compareTo(Covariant o); }