我有一个很奇怪的行为。
我在名为 pmd-extensions.xml 的文件中为 PMD 4.2.6 创建了 XPath 规则:
...
<rule name="AvoidPrintStackTrace-XPath"
message="Avoid to use printStackTrace - XPath"
class="net.sourceforge.pmd.rules.XPathRule">
<description>Avoid to use printStackTrace - XPath</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Name[contains (@Image, "printStackTrace")]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
// don't do this!
myException.printStackTrace()
]]>
</example>
</rule>
...
我有一个单元测试验证这个规则并且工作正常:
...
@Before
public void setUp() {
addRule("rulesets/pmd-extensions.xml", "AvoidPrintStackTrace-XPath");
}
...
但是当我在 Sonar 中嵌入这条规则时,这条规则并没有触发任何违规行为,而我期望的是:
...
<rule key="AvoidPrintStackStrace-XPath" >
<name>AvoidPrintStackStrace-XPath</name>
<configKey>rulesets/pmd-extensions.xml/AvoidPrintStackTrace-XPath</configKey>
<category name="Usability"/>
<description>Avoid to use printStackTrace - XPath</description>
</rule>
...
如果我直接在 Sonar 中声明此规则(使用相同的 XPath 表达式),该规则会按预期触发违规:
...
<rule key="AvoidPrintStackStrace-XPath-Sonar" priority="MAJOR">
<name><![CDATA[AvoidPrintStackStrace-XPath-Sonar]]></name>
<configKey><![CDATA[net.sourceforge.pmd.rules.XPathRule]]></configKey>
<category name="Maintainability"/>
<description>Avoid to use printStackTrace - XPath-Sonar</description>
<param key="xpath" type="s">
<description><![CDATA[XPath expressions.]]></description>
<defaultValue>//Name[contains (@Image, "printStackTrace")]</defaultValue>
</param>
<param key="message" type="s">
<description><![CDATA[Message to display when a violation occurs.]]></description>
<defaultValue>Prevent use of printStackTrace</defaultValue>
</param>
</rule>
...
怎么了 ?
谢谢。