如何使用PMD忽略短变量规则中的"id"

San*_*rDN 9 java eclipse pmd

我正在使用PMD插件(版本4.0.2)用于Eclipse(Eclipse Kepler Java EE).我已经配置了一个命名规则:ShortVariable.

除了像"id"和之类的参数外,这种方法很好"e".我希望PMD忽略这些.所以我搜索了一种忽略某些参数的方法.我找到了这个链接(尽管它是用于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>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用eclipse插件导入此规则集时,它显示没有可能导入的规则.有任何想法吗?

San*_*rDN 14

我在这里找到了解决问题的方法.

生成的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) &lt; 3) and (not (@Image='id'))]
                    [not(ancestor::ForInit)]
                    [not((ancestor::FormalParameter) and (ancestor::TryStatement))]
                </value>
            </property>
        </properties>
    </rule>
</ruleset>
Run Code Online (Sandbox Code Playgroud)

为了能够忽略更多变量名称,请重复以下部分:

and (not (@Image='myVariableToIgnore'))
Run Code Online (Sandbox Code Playgroud)