PMD is a pretty neat little tool that analyzes Java source code and outputs a number of informative and sometimes alarming measurements. In its own words,
PMD scans Java source code and looks for potential problems like:
- Possible bugs – empty try/catch/finally/switch statements
- Dead code – unused local variables, parameters and private methods
- Suboptimal code – wasteful String/StringBuffer usage
- Overcomplicated expressions – unnecessary if statements, for loops that could be while loops
- Duplicate code – copied/pasted code means copied/pasted bugs
This is pretty useful, especially in conjunction with beastly projects with a large volume of code. More interestingly it also analyzes cyclomatic complexity, which while not all that useful in and of itself, is nonetheless an interesting metric.
Since it comes as a *NIX binary, PMD is also highly scriptable:
#!/bin/bash
# batch_pmd.sh
OUTTYPE="html" # |xml|text
RULES=("basic,braces,codesize,clone,controversial,coupling,design,finalizers,imports,naming,optimizations,strictexception,strings,unusedcode")
for x in *.java;
do
echo "Checking $x:"
profiler/bin/pmd.sh $x $OUTPUTTYPE $RULES;
done;