Wednesday, April 08, 2009

Filtering in FindBugs

Here is an example findbugs exclusion filter which is generally useful for most projects. I could not seem to find a good example of something general anywhere so hopefully this will be a helpful reference to others. If you placed an xml file like this into your project in eclipse you can configure findbugs to use it as an exclusion filter.

Here is the sample xml (escaped):
<FindBugsFilter>
<!-- filter out test classes with medium (2) or low (3) warnings -->
<Match>
<Or>
<Class name="~.*\.AbstractTest.+" />
<Class name="~.*Test" />
</Or>
<Bug category="PERFORMANCE,MALICIOUS_CODE,STYLE,SECURITY" />
</Match>

<!-- remove the rules which require all passed vars to be immutable -->
<Match>
<Bug code="EI,EI2" />
</Match>

<!-- Filter out certain categories of bugs -->
<Match>
<Bug category="STYLE" />
</Match>
</FindBugsFilter>

To make this work in eclipse (with findbugs plugin installed) just do the following:
  1. Create an xml file in your project with the contents from above
  2. Right click the project name and select Properties
  3. Choose FindBugs
  4. Check the box marked Run FindBugs automatically
  5. Click on the Filter files tab at the top
  6. Click the Add button next to Exclude filter files:
  7. Select the xml file you created
  8. Click OK
You can adjust the filter using the findbugs guide and the full list of bug types and categories is available.

No comments: