0
我有一個相當奇怪的行爲。自定義XPath規則在pmd中引發違規,但在聲納中不存在
我創建PMD 4.2.6在文件命名爲XPath的規則PMD-extensions.xml:
...
<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");
}
...
但是,當我嵌入聲納這個規則,該規則不會觸發任何侵犯,而我的預期之一:
...
<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>
...
如果我DECL此規則直接聲納(有相同的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>
...
有什麼不對?
謝謝。
我勾選了配置文件中的規則,並且此配置文件用於分析,所以這不是原因(實際上嵌入在JAR中的其他自定義規則會被調用並觸發違規,而此規則不會)。 –