26.15. FindBugs

26.15.1. Overview

The FindBugs Sensor is an Ant task that sends indications potential bugs in Java code identified by FindBugs to the Hackystat server. The FindBugs sensor represents this metric data using the CodeIssue sensor data type, documented in Section 25.6, “CodeIssue”

FindBugs is a tool that automates the process of checking Java code to find bugs. It uses a concept called bug patterns. Bug patterns can be caused by difficult language features, misunderstood API methods, misunderstood invariants when code is modified during maintenance, and many other common mistakes. Table 26.8, “FindBugs Metrics” lists the information gathered by the FindBugs sensor and the languages for which they are collected.

Table 26.8. FindBugs Metrics

MetricDescription
Source FileThe fully qualified name of the source file that was analyzed by findbugs.
ErrorThe severity, message, and type of coding standard violation.

[Note]Note

Currently, this sensor sends information to the Hackystat server only when a source file contains coding standard violations. Thus, the Hackystat server analyses cannot distinguish between files that have no coding standard violations and files that have not had this tool invoked on them. This means that this CodeIssue data is potentially susceptible to "false negatives" during analysis.

26.15.2. Installation

26.15.2.1. Prerequisites

The FindBugs sensor requires Java 1.5.0 or later, Ant 1.6.5 or later, and FindBugs 0.94 or later.

26.15.2.2. Download and install FindBugs

Download and install FindBugs from its home page. Section 4.12, “Measurement: Source code quality with FindBugs” provides a recommended approach to FindBugs installation for use with Hackystat.

26.15.2.3. Download HackyInstaller and set your Hackystat Host and User Key

Follow the instructions in Chapter 2, Client-side configuration: Tool sensor installation to set your Hackystat host and user key.

26.15.2.4. Configure and install the FindBugs sensor

In the HackyInstaller main window, select the FindBugs sensor and press "Configure Selected Sensor". Figure 26.29, “ FindBugs sensor installer configuration window ” shows what the configuration window should look like after the path to Ant is set, the sensor is enabled, and the "Install" button has been pressed to install the sensor.

Figure 26.29.  FindBugs sensor installer configuration window


FindBugs sensor installer configuration window

This sensor supports the following properties and paths:

  • Enable FindBugs Sensor: This property controls whether the sensor is active or not. If not checked, the sensor will not collect or send data even if installed.

  • Ant home directory: This path specifies the top-level directory for Ant. The FindBugs sensor requires the placement of a sensor executable into Ant's lib/ directory.

26.15.2.5. Define Ant targets that run the FindBugs tool and the FindBugs sensor

Example 26.35, “Example findbugs.tool, findbugs.report, and findbugs.sensor targets” illustrates one possible definition of these targets.

Example 26.35. Example findbugs.tool, findbugs.report, and findbugs.sensor targets

  <target name="findbugs.tool" depends="compile"
    description="Runs FindBugs over code to check for problems.">
    <!-- Fail this target if FindBugs is not installed. -->
    <available file="${env.FINDBUGS_HOME}/lib/findbugs.jar" property="findbugs.available"/>
    <fail unless="findbugs.available" message="Error: FINDBUGS_HOME not set or findbugs.jar not found."/>
    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"  classpath="${env.FINDBUGS_HOME}/lib/findbugs-ant.jar" />

    <!-- Run FindBugs. -->
    <mkdir dir="${findbugs.dir}"/>
    <findbugs 
        home="${env.FINDBUGS_HOME}" 
        output="xml:withMessages" 
        outputFile="${findbugs.dir}/findbugs.xml" >
      <auxClasspath>
        <fileset file="${ant.home}/lib/junit.jar"/>
      </auxClasspath>
      <sourcePath>
        <fileset dir="${src.dir}">
          <include name="**/*.java" />
        </fileset>
      </sourcePath>
      <class location="${build.dir}/classes" />
    </findbugs>
  </target>

  <target name="findbugs.report" description="Generate an HTML report on FindBugs.">
    <xslt in="${findbugs.dir}/findbugs.xml" 
          style="${env.FINDBUGS_HOME}/src/xsl/default.xsl" 
          out="${findbugs.dir}/findbugs-default.html" />
  </target>

  <target name="findbugs.sensor" description="Sends CodeIssue data to Hackystat using the Findbugs sensor.">
    <!-- Define the findbugs sensor taskdef, failing the build if the sensor is not installed. -->
    <available classname="org.hackystat.sensor.findbugs.FindBugsSensor" property="findbugs.sensor.available"/>
    <fail unless="findbugs.sensor.available" message="Error: Findbugs sensor not installed."/>
    <taskdef name="hacky-findbugs" classname="org.hackystat.sensor.findbugs.FindBugsSensor" />
    
    <!-- Send the code issue information to the hackystat server. -->
    <hacky-findbugs verbose="true">
      <fileset file="${findbugs.dir}/findbugs.xml"/>
    </hacky-findbugs>
  </target>

For more details on these targets, see Section 4.12, “Measurement: Source code quality with FindBugs”, which illustrates the installation and use of FindBugs on a sample Java system called StackyHack.

26.15.3. Installation verification

26.15.3.1. Verify verbose mode output

To verify your FindBugs sensor installation, invoke the target and check to see that data is sent to the server. Example 26.36, “Example FindBugs output” shows the shell output from an example run with verbose= "on".

Example 26.36. Example FindBugs output


C:\svn-csdl\StackyHack>ant findbugs
Buildfile: build.xml

installBuildSensor:

init:

checkstyle:

compile:
    [mkdir] Created dir: C:\svn-csdl\StackyHack\build\classes
    [javac] Compiling 5 source files to C:\svn-csdl\StackyHack\build\classes

findbugs:
    [mkdir] Created dir: C:\svn-csdl\StackyHack\build\findbugs
 [findbugs] Running FindBugs...
 [findbugs] Warnings generated: 1
 [findbugs] Output saved to C:\svn-csdl\StackyHack\build\findbugs/findbugs.xml
     [xslt] Processing C:\svn-csdl\StackyHack\build\findbugs\findbugs.xml to C:\svn-csdl\StackyHack\build\findbugs\findbugs-default.html
     [xslt] Loading stylesheet C:\java\findbugs-0.9.3\src\xsl\default.xsl
     [xslt] Processing C:\svn-csdl\StackyHack\build\findbugs\findbugs.xml to C:\svn-csdl\StackyHack\build\findbugs\findbugs-plain.html
     [xslt] Loading stylesheet C:\java\findbugs-0.9.3\src\xsl\plain.xsl
     [xslt] Processing C:\svn-csdl\StackyHack\build\findbugs\findbugs.xml to C:\svn-csdl\StackyHack\build\findbugs\findbugs-fancy.html
     [xslt] Loading stylesheet C:\java\findbugs-0.9.3\src\xsl\fancy.xsl
     [xslt] Processing C:\svn-csdl\StackyHack\build\findbugs\findbugs.xml to C:\svn-csdl\StackyHack\build\findbugs\findbugs-summary.html
     [xslt] Loading stylesheet C:\java\findbugs-0.9.3\src\xsl\summary.xsl
[hacky-findbugs] Sensor enabled?: true
[hacky-findbugs] Processing file: C:\svn-csdl\StackyHack\build\findbugs\findbugs.xml
[hacky-findbugs] Guessed fileName from srcDir=C:\svn-csdl\StackyHack\src and className=edu.hawaii.stack.Stack
[hacky-findbugs] fileName=C:\svn-csdl\StackyHack\src\edu\hawaii\stack\Stack.java
[hacky-findbugs] Hackystat data on 1 Code Issues sent to http://hackystat.ics.hawaii.edu/ (0 secs.)

BUILD SUCCESSFUL
Total time: 14 seconds
C:\svn-csdl\StackyHack>

The FindBugs sensor will output a different message if the data was not sent successfully. The data was sent in less than a second, hence the "0 secs." output value.

26.15.3.2. List Sensor Data

Once you verify that FindBugs data is being sent from the client using verbose mode, login to your account on your Hackystat server, and use the "List Sensor Data" command on the Extras page to verify that FileMetric data for today's date was received by the server for the FindBugs tool, as illustrated in Figure 26.30, “ List Sensor Data with FindBugs data ”.

Figure 26.30.  List Sensor Data with FindBugs data


List Sensor Data with FindBugs data

26.15.4. Installation troubleshooting

26.15.4.1. Troubleshooting general client-server problems

The first step in troubleshooting your sensor installation is to verify that your Hackystat host and key settings are valid and that you can contact the Hackystat server, as described in Section 2.4, “HackyInstaller GUI: Setting and verifying the Hackystat host and user key”

26.15.4.2. Checking client-side logging

The FindBugs sensor writes out a file called findbugs.0.log to the .hackystat/logs directory that can be useful in debugging your installation. Under normal conditions, this file should look similar to Example 26.37, “findbugs.0.log file for FindBugs sensor”.

Example 26.37. findbugs.0.log file for FindBugs sensor

Hackystat Version: 7.0.1201 (December 1 2005 14:18:13)
SensorShell started at: 12/04/2005 18:17:49
Type 'help' for a list of commands.
Host: http://hackystat.ics.hawaii.edu/ is available and key is valid.
Defined shell command: Dependency
Defined shell command: Issue
Defined shell command: CodeIssue
Defined shell command: EvolSdt
Defined shell command: Perf
Defined shell command: FileMetric
Defined shell command: ReviewIssue
Defined shell command: Activity
Defined shell command: Cli
Defined shell command: ReviewActivity
Defined shell command: Coverage
Defined shell command: UnitTest
Defined shell command: BuffTrans
Defined shell command: Commit
Defined shell command: Build
#> AutoSend [10]
AutoSend OK (set to 10 minutes)
AutoSend enabled every 10 minutes.
Checking for offline data to recover.
No offline data found.
#> CodeIssue [set, tool=FindBugs]
set OK
#> CodeIssue [add, runtime=1133756269757, fileName=C:\java\svn-csdl\StackyHack\src\edu\hawaii\stack\Stack.java, type=DLS_DEAD_LOCAL_STORE, data=severity=2,message=Dead store to local variable,line=76,column=className=edu.hawaii.stack.Stack,category=STYLE]
CodeIssue add OK (1)
#> send
Sending sensor data (12/04 18:17:50)
  Ping: Ping OK (contacted server http://hackystat.ics.hawaii.edu/ with valid key.)
  Dependency: Send OK (No entries to send.)
  AutoSend: AutoSend OK ('send' command ignored)
  Issue: Send OK (No entries to send.)
  CodeIssue: Send OK (1 entries)
  EvolSdt: Send OK (No entries to send.)
  Perf: Send OK (No entries to send.)
  FileMetric: Send OK (No entries to send.)
  ReviewIssue: Send OK (No entries to send.)
  Activity: Send OK (No entries to send.)
  Cli: Send OK (No entries to send.)
  ReviewActivity: Send OK (No entries to send.)
  Coverage: Send OK (No entries to send.)
  Commit: Send OK (No entries to send.)
  BuffTrans: Send OK (No entries to send.)
  UnitTest: Send OK (No entries to send.)
  Build: Send OK (No entries to send.)

26.15.4.3. Submit a trouble report

If none of the above troubleshooting activities solves your problem, then you should send an email to your Hackystat Administrator to request help. Please include in your email the following information:

  • The output from 'java -version'.

  • The output from 'java -jar sensorshell.jar -verify'.

  • The contents of your sensor.properties file.

  • The contents of the findbugs.0.log file.

  • A description of the problem you are having.

26.15.5. Usage tips

26.15.5.1. Integrate FindBugs data collection into your development process

You probably don't want to have to invoke "ant findbugs" manually each time you want metric data sent to Hackystat. It is better to have the findbugs data calculated and sent to Hackystat automatically as a natural course of development. One strategy is to make the findbugs target a dependent target of an "install" or "distribution" target so that structural data is recorded each time significant changes occur to the system. This provides a historical record of the structural evolution that can be later used by Hackystat analyses.

26.15.5.2. Disable verbose mode

Set verbose="off" once things are working correctly.

26.15.6. Uninstallation

To disable FindBugs data collection temporarily, invoke HackyInstaller, bring up the FindBugs configuration window, and uncheck the "Enable FindBugs sensor" property, and apply the setting. To permanently uninstall the FindBugs sensor, press the "Uninstall" button and remove the Build targets from your Ant build.xml files.