2013-12-23 46 views
8

我正在为Eclipse(Eclipse Kepler Java EE)使用PMD插件(版本4.0.2)。我已经配置了一个命名规则:ShortVariable如何在使用PMD的短变量规则中忽略“id”

这工作正常,除了参数如"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插件,这个规则集,它没有显示出可能的规则导入。 任何想法?

+0

[找到解决方案](http://zavyn.blogspot.be/2011/09/solution-modify-pmds-shortvariable-rule.htm l)(也许不是最大的)经过一番搜索。 – SanderDN

+1

您的链接解决方案非常好!请将其张贴为答案,并在宽限期后接受。谢谢。 –

回答

11

我找到了我的问题解决方案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')) 
+1

您需要将标记放在标记内。 –

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> 
+1

不适用于PMD 5.1.3: java.lang.IllegalArgumentException:无法在Rule ShortVariable上设置不存在的属性'exceptions' – SanderDN

+0

对不起,我使用PHPMD 2.2.3,我没有意识到它是Eclipse(JAVA世界)问题。 – Simon