Question

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...

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top