PMD 找不到规则 UseSingleton

PMD can't find rule UseSingleton

由于某些奇怪的原因,PMD 给出了这个错误:

An internal error occurred during: "ReviewCode".
    Unable to find referenced rule UseSingleton; perhaps the rule name is mispelled?

我的 ruleset.xml:

中是这样声明这条规则的
   <rule ref="rulesets/java/design.xml/UseSingleton"/>

我使用的是带有 4.0.6 PMD 插件版本的 Eclipse Kepler。我究竟做错了什么?

您尝试使用 PMD >= 5.0 的配置。对于较低版本,您需要从规则参考中删除 java/

<rule ref="rulesets/design.xml/UseSingleton"/>

来自 PMD 5.0 发行说明:

This version of PMD breaks API compatibility with prior versions of PMD, as well as RuleSet XML compatibility. Also the maven coordinates (groupId) have been changed. The decision to break compatibility, allows PMD internals and code organization to be improved to better handle additional languages.

另请参阅错误报告中的答案 #1339 and the changelog for 5.3.1

Language Java, ruleset design.xml: The rule “UseSingleton” has been renamed to “UseUtilityClass”. See also bugs #1059 and #1339.

PMD 5.3.1 及更高版本修复了此问题。

目前,您需要手动修复规则集文件并将 UseSingleton 替换为 UseUtilityClass。我建议关闭 eclipse,在您的工作区找到 ruleset.xml 文件,更改它,然后启动 eclipse。