gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:34:31 24 4
gpt4 key购买 nike

我正在为 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com