Friday, June 10, 2005

NetBeans plugins I use, part 2

WARNING: This blog entry was imported from my old blog on (which used different blogging software), so formatting and links may not be correct.

PMD logo

In my first favorite plugin
I described Jalopy. The other external plugin I use is PMD.

PMD is a source code analysis tool which parses java source code, and then
runs a large number of rules over the source code. For example, it checks
for program errors, like double checked locking patterns, or overriding
equals without overriding hashCode. It checks
for potential program errors, like having an equals method
with a single parameter that is not of type Object. And it
checks for a number of coding style "violations", such as for example
failing to use uppercase letters for class constants, or using excessively
long or short variable names.

Here's a screenshot of PMD in action: you select a class or a package and
run PMD on it, and all the rule violations are listed in the output window,
where you can click to bring up the relevant source (or hit F12 and Shift F12 repeatedly
to iterate forwards and backwards in the list.)

Thankfully, the ruleset is configurable. That's important, because I the
default set of rules you get is not particularly good. It includes a rule
I truly hate (and because I disagree with the result, I violate it all
over my code, with the result that unless I customize the ruleset I get
a bajillion rule violations when I run PMD on my code. I might write a rant
about that rule in another blog entry...)

Customizing the ruleset is easy, once you know where to find the
dialog: Tools | Options | IDE Configuration | Server and External Tool Settings | PMD Settings. Open the Rules customizer in the property sheet,
and you get the following dialog:

Here you can move available rules to and from your chosen set of rules.
Try adding all at first, then removing rules that get flagged in your code
that you don't care about, or that you disagree with. More importantly, you
can click on each rule to see a description and example of the kind of code
that triggers the violation. This is really useful if you want to see if this
is a rule you want to check for - and to get the rationale from the person
who contributed the rule.

Note that you cannot get it to be happy if you use all the rules, since
there are contradictory rules. For example, if your bean class has no
constructor, you get the AtLeastOneConstructor violation telling
you your class needs at least one constructor. But once you add one, the
UnnecessaryConstructor rule will tell you
that a constructor is not necessary when it's public and has no arguments!
So learning how to configure the ruleset is essential.

I've been following PMD for quite a while. A couple of years ago
I wrote a plugin that took the PMD integration a bit further in the IDE.
I had written a suggestions window for NetBeans, and I plugged PMD
into it, along with automatic fixers for some of the rules. So for example,
if PMD flagged a method as unused, you could just double click to get
a removal preview dialog, and then click again to nuke the method.
Here's a screenshot of this in action on an unused field:

As you can see, this screenshot is old - look at the old icon style, or the old property sheet,
and the old window manager... I think this was in NetBeans 3.4.
(More suggestions window screenshots here)

Unfortunately, this was a hobby project, and I suddenly got
extremely busy working on
since we were trying to design
and develop it with a suicidal schedule, so my spare time become occupied
by work instead. I haven't been able to keep it up, and now it looks like
it will take some work to bring it up to speed. However, in the meantime
part of the suggestions framework has been incorporated into standard
NetBeans (specifically the TODO scan portion of it), and I saw some
comments on the dev@tasklist alias that Jan Jancura is going to do some
work in this area so perhaps we can get this going again.

It also looks like the PMD plugin itself on SourceForge is getting some
attention with some recent development, so I think this going to get
even better soon.

To download the plug-in, visit
this link,
download the most recent release, unzip it, and then from the Tools | Update Center choose
to install a locally downloaded nbm file.


  1. [Trackback] Tor Norbye has done a writeup on PMD. Basically it looks for common coding errors in your Java source, for example switch statements without default labels or use of the broken double checked locking pattern. It looked interesting, so I installed it i...

  2. Henrik LynggaardJune 11, 2005 at 7:44 AM

    I have tried it after your recomendations, but I cannot get it to work with java5 source code :-\
    Or am I doing something wrong ?
    Checkstyle 4.0 beta supports java5 so I guess I am back to using it.

  3. The PMD supports 1.5 source files. Unfortunately NetBeans plugin does not pass the source level yet so 1.4 is assumed. It is improved in CVS version and new release with this fix can follow soon. Please be patient.

  4. Excellent! Glad to hear it Tom. I guess it's time to run PMD over my sources again!!