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

标签 java eclipse pmd

我正在为 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) &lt; 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/

相关文章:

eclipse - 在 Eclipse 中,我可以为特定文件类型覆盖 "spaces for tabs"吗?

java - 我们可以对 Sonar 的 PMD 插件和 Eclipse 的 PMD 插件使用相同的抑制警告吗?

java - FST(有限状态传感器)库,C++ 或 java

java - 从另一个包启动 Activity - 问题

java - Spring Data + Mongo列表嵌套复杂类型在mongo中使用id作为属性

java - 从应用程序内永久添加按钮

java - Eclipse 系统属性

java - 如何让 PMD 在 Maven 构建开始而不是结束时运行?

java - 逐行迭代文本文件的内容 - 是否有最佳实践? (与 PMD 的 AssignmentInOperand 相比)

java - 使用 Moxy 处理动态元素名称