Static Code Analysis & Findbug: Shihab KB
Static Code Analysis & Findbug: Shihab KB
Static Code Analysis & Findbug: Shihab KB
FindBug
Shihab KB
Agenda
To share some thoughts about static code
analysis.
We have good practices for finding bugs early. (Unit testing, pair
programming, code review etc)
No body is perfect.
Clearly a mistake
but in practice, adapters is probably never null
Impact
we would probably notice a null pointer exception
When should we perform
Static analysis
Static analysis earlier is better
Example
Null Pointer
Referencing a null value results in NullPointerException
Examples
// Eclipse 3.0.0M8
Control c = getControl();
if (c == null && c.isDisposed())
return;
// Eclipse 3.0.0M8
String sig = type.getSignature();
if (sig != null || sig.length() == 1) {
return sig;
}
More Null Pointers
// JDK 1.5 build 42
if (name != null || name.length > 0) {
javax.security.auth.kerberos.KerberosTicket, 1.5b42
// flags is a parameter
// this.flags is a field
if (flags != null) {
if (flags.length >= NUM_FLAGS)
this.flags = ...
else
this.flags = ...
} else
this.flags = ...
if (flags[RENEWABLE_TICKET_FLAG]) {
Doomed Equals
if
(“com.sun.java.swing.plaf.WindowsLookAndFeel”
.equals(UIManager.getLookAndFeel())) {
…
}
Requirements
http://findbugs.sourceforge.net/downloads.html
Unzip the downloaded file. That’s all. You can double click the
findbugs.bat file from \findbugs-1.3.9\bin folder
Using the FindBugs GUI
Choose class’s
location here
Choose source
code’s location here
Using the FindBugs GUI
Using the FindBugs™ Eclipse
plugin
Requirements
To use the FindBugs Plugin for Eclipse, you need
Eclipse 3.3 or later, and JRE/JDK 1.5 or later.
Goto Help->Software Updates (See next
slide)
You can also manually download the plugin
and extract it in Eclipse's "plugins"
subdirectory.
Using the FindBugs™ Eclipse
plugin
FindBugs command line
findbugs -textui -xml rt.jar >rt.xml
FindBugs Annotations
Annotations in Java is all about
adding meta-data facility to the Java
Elements. Like Classes, Interfaces or
Enums, Annotations define a type in
Java and they can be applied to
several Java Elements.