Custom PMD Rule - Check Package Comment In Place

爷,独闯天下 提交于 2019-12-08 06:49:55

问题


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:


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.



来源:https://stackoverflow.com/questions/2166661/custom-pmd-rule-check-package-comment-in-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!