A perfect use for Annotations

04 Oct 2005

You've always been able to turn off PMD warnings with the "NOPMD" feature, for example:

if (something) {  // NOPMD
 // empty if statement, blah, but the NOPMD will tell PMD to skip it
}

But now there's a better way. If you want to turn off warnings for an entire class, you can use the Java standard annotation java.lang.SuppressWarnings:

@SuppressWarnings("")
public class Foo {
 // lots of code, no PMD reports!
}

PMD will recognize that annotation and, well, suppress any warnings in that class. Much cleaner!

There's still more work to do, since the SuppressWarnings annotation can be also applied to fields, methods, constructors, parameters, and local variables. Also, it'd be nice to be able to only suppress warnings from specific rules. But anyhow, it's a good start.

Some notes I made while coding this up are here, and you can download an updated pmd-3.3.jar file here that includes this feature.

Thanks to Mark Levison for prodding me to get working on this feature!