Friday, August 13, 2004

Java code analysis tools

Today I stumbled over some stupid errors and thought "why doesn't eclipse tell me about that?" so I remembered Findbugs and was first looking for a findbugs output xml to html stylesheet (there is one posted in the forum at sf.net). I also saw that there is an eclipse plugin. So I was trying to get findbugs with its plugin to work together with Eclipse 3.0, but did not have success because I misunderstood part of the the documentation.
While googling for help, I came across PMD and thought to give it a try.
The plugin works nicely and is easy to set up and run.
But after running it over my source file, I was disappointed, because it mostly was a different incarnation of checkstyle. It was telling me that some variable names were too short, others too long bla bla bla.
But it did not find the real problem of the following code snippet:

Object o1 = getBla(..);
Object o2 = getBla(..);
if (o1==null && o2 !=null) {
// do something
}
else {
if (!o1.equals(o2)) // something else
}

PMD complains that o1 and o2 are too short that is all, but does not detect the NPE when o1 and o2 are both null.

Ok, this might be too tricky. Lets try something simpler:

Object o = null;
System.out.println("below is a NPE");
o.equals(null);

Even here, PMD only complains about the short variable name, but doesn't find the NPE.

I am sure, that a tool like PMD can help a lot in improving code quality, but is basically useless for bug hunting.

And for my need to convert the findbugs output into a html page, I wrote an extremly simple xslt stylesheet.

No comments:

Post a Comment