Monday, October 05, 2009

Findbugs Filter Creation Tool

We have started using findbugs on some of the projects I am working on. It seems pretty useful, and it has picked out some bugs already. It does however, report quite a few bugs that are not a problem. Luckily, findbugs provides filters to be able to exclude the bugs you don't want to include.

After spending a day on creating the filter files manually, I found it to be a bit fiddly and time-consuming to create these by hand. I tried looking to see if there was a way to automate this a bit more. I might be wrong, but I could not find a tool to do this. Hopefully the below will help others in the same situation.

When you run a findbugs check it generates an output file. I do this via the maven plugin: mvn findbugs:check which yields a file called findbugsCheck.xml. This file references an xsl stylesheet:
<?xml-stylesheet type="text/xsl" href="http://findbugs.sourceforge.net/xsl/default.xsl"?>

so if you open this file in your browser you will see something like this:


I created another version of default.xsl called filterHelper.xsl. Modifying findbugsCheck.xml to use this stylesheet:
<?xml-stylesheet type="text/xsl" href="http://anonsvn.jboss.org/repos/jbossas/projects/findbugs-filtercreator/trunk/filterHelper.xsl"?>

gives output that looks much the same

Now each bug has a a checkbox next to it. Check the bugs you want to create an exclude filter for, press the "Create Filter" button and the textarea gets populated with the Match elements for each bug.

You can then copy the results into a file, in my projects they are under src/main/resources/findbugs/exclude.xml. For example:
<FindBugsFilter>
<Match>
<Class name="org.jboss.system.ServiceMBeanSupport"/>
<Field name="SERVICE_CONTROLLER_SIG"/>
<Bug pattern="MS_PKGPROTECT"/>
</Match>
<!-- Rest of exclusions here -->
</FindBugsFilter>

Then set maven up to use the filter in the plugins section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<excludeFilterFile>${project.build.outputDirectory}/findbugs/exclude.xml</excludeFilterFile>
<debug>true</debug>
</configuration>
</plugin>