首页 文章

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

提问于
浏览
8

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

除了 "id""e" 之类的参数之外,这种方法很好 . 我希望PMD忽略这些 . 所以我搜索了一种忽略某些参数的方法 . 我找到this link(虽然它似乎让它工作 . 我的配置文件看起来像这样(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插件导入此规则集时,它显示没有可能的导入规则 . 有任何想法吗?

2 回答

  • 3

    下面的XML对PHP工具PHPMD 2.2.3有效

    <?xml version="1.0"?>
    <!DOCTYPE ruleset>
    <ruleset
        name="My PMD ruleset for symfony 2.5"
        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"
    >
    
        <rule ref="rulesets/unusedcode.xml" />
        <rule ref="rulesets/codesize.xml" />
        <rule ref="rulesets/cleancode.xml" />
        <rule ref="rulesets/controversial.xml" />
        <rule ref="rulesets/design.xml" />
        <rule ref="rulesets/naming.xml">
           <exclude name="ShortVariable" />
        </rule>
        <rule ref="rulesets/naming.xml/ShortVariable">
            <properties>
                <property name="exceptions" value="id,em" />
            </properties>
        </rule>
    </ruleset>
    
  • 12

    我找到了解决问题的方法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) &lt; 3) and (not (@Image='id'))]
                        [not(ancestor::ForInit)]
                        [not((ancestor::FormalParameter) and (ancestor::TryStatement))]
                    </value>
                </property>
            </properties>
        </rule>
    </ruleset>
    

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

    and (not (@Image='myVariableToIgnore'))
    

相关问题