我正在为 Eclipse (Eclipse Kepler Java EE) 使用 PMD 插件(版本 4.0.2)。我配置了一个命名规则:ShortVariable .
除了像 "id"
和 "e"
这样的参数,这工作正常。我希望 PMD 忽略这些。所以我寻找一种方法来忽略某些参数。我找到了 this link (虽然它是用于 phpmd 的)并尝试过,但我似乎无法让它工作。我的配置文件如下所示 (XML):
<?xml version="1.0"?>
<ruleset name="My PMD ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
My PMD
</description>
<rule ref="rulesets/java/naming.xml/ShortVariable">
<property name="exceptions" value="id" />
</rule>
</ruleset>
当我尝试使用 eclipse 插件导入此规则集时,它显示没有可导入的规则。 有什么想法吗?
最佳答案
我找到了解决问题的方法 here .
生成的 xml 如下所示:
<?xml version="1.0"?>
<ruleset name="My PMD ruleset"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
My PMD
</description>
<rule ref="rulesets/java/naming.xml/ShortVariable">
<properties>
<property name="xpath">
<value>
//VariableDeclaratorId[(string-length(@Image) < 3) and (not (@Image='id'))]
[not(ancestor::ForInit)]
[not((ancestor::FormalParameter) and (ancestor::TryStatement))]
</value>
</property>
</properties>
</rule>
</ruleset>
为了能够忽略更多的变量名,重复以下部分:
and (not (@Image='myVariableToIgnore'))
关于java - 如何使用PMD忽略短变量规则中的 "id",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741541/