views:

218

answers:

1

I'm trying to write a rule to enforce that a package contains a Javadoc comment with a particular expression, e.g:

/**
 * Example Expression
 */

Does anybody know how I would write such a rule using the AbstractJavaRule class. I've looked at ASTPackageDeclaration, but this doesn't appear to have what I want.

Many thanks in advance...

+1  A: 

PMD uses JavaCC to parse each java file into an Abstract Syntax Tree. Typically parsers used for compilation just drop comments, and from what I can gather on the "How to write a rule" page, PMD is also dropping comments from the AST.

But looking at the PMD forum, it does look like comments are preserved as a SPECIAL_TOKEN, but it is difficult to align them with the AST node they are associated with. So you may have a hard time writing this rule. Have you looked at Checkstyle instead? It has a RegexpHeader rule that may do what you want.

TheDon
Great response, thanks. I'll take a look today and see if I can get the comments with PMD. I saw in the Design Rules section there's a check for UncommentedEmptyMethod. Looks promosing, failing that I'll drop to Checkstyle and try that.
Jon