Monday, July 23, 2007


As I was perusing the Google Code site, I came across a really cool tool called FindBugs™. FindBugs™ is an open source Java debugging tool that scans through all of your Java class and source files to find programmer-error bugs. From their fact sheet:

"FindBugs looks for bugs in Java programs. It is based on the concept of bug patterns. A bug pattern is a code idiom that is often an error. Bug patterns arise for a variety of reasons:

* Difficult language features
* Misunderstood API methods
* Misunderstood invariants when code is modified during maintenance
* Garden variety mistakes: typos, use of the wrong boolean operator

FindBugs uses static analysis to inspect Java bytecode for occurrences of bug patterns. Static analysis means that FindBugs can find bugs by simply inspecting a program's code: executing the program is not necessary. This makes FindBugs very easy to use: in general, you should be able to use it to look for bugs in your code within a few minutes of downloading it. FindBugs works by analyzing Java bytecode (compiled class files), so you don't even need the program's source code to use it. Because its analysis is sometimes imprecise, FindBugs can report false warnings, which are warnings that do not indicate real errors. In practice, the rate of false warnings reported by FindBugs is less than 50%."

You can use FindBugs as a standalone Java WebStart application, or as a (very handy) plugin to Eclipse. Very cool. I wanted to test this out on something, and what better source than the OpenMRS codebase? I grabbed the current alpha branch and tested both the WebStart application and the Eclipse plugin. The Eclipse plugin found 985 bugs and proved itself to be quite useful.

Here are instructions for using the FindBugs Eclipse plugin on the OpenMRS codebase (although this will work for any Eclipse project).

Install the FindBugs Plugin

1. Fire up Eclipse. Go to Help > Software Updates > Find and Install.
2. Select "Search for new features to install" and click Next.
3. Click "New Remote Site" and enter "FindBugs update site" for the Name and for the URL and click OK:

4. Make sure "FindBugs update site" is checked and click Finish.
5. Expand the trees in the next window and check "FindBugs Feature" and click Next.
6. Accept the license and click Next.
7. Click Finish, follow any prompts that may come up, and restart Eclipse when prompted.

Using FindBugs on Your Project

1. If the Java files you want to debug aren't already added to Eclipse, create a new project via File > New > Project.
2. Once your project is in Eclipse, right click on the project name and go to Find Bugs > Find Bugs:

3. Let FindBugs work its magic. When it's done, all of the bugs found will be listed as Warnings in Eclipse's Problems view.
4. Opening FindBugs' view is much more useful than the Problem view. Go to Window > Show View > Other, then expand the FindBugs tree and select Bug Tree View:

5. This creates a nifty little view with all of the bugs found, categorized by bug type:

6. And now the really cool part: Click on an individual bug, and it will jump to that line of source where you can fix your code:

7. Impress the world by debugging all of your code, and then debug all of the world's as well.

I hope this is useful!

1 comment:

dost said...
This comment has been removed by the author.