我有以下课程:
import javax.annotation.PostConstruct;
public class PmdUnusedMethod {
private void unusedMethod() {
}
@PostConstruct
private void postConstructAnnotatedMethod() {
}
}
并定义了 PMD 规则集:
<rule ref="rulesets/java/unusedcode.xml"/>
在这种情况下,PMD 向我报告两个有关未使用方法的错误(“unusedMethod”和“postConstructAnnotatedMethod”),但我想忽略使用 @PreDestroy 和 @PostConstruct 注释的方法的规则“UnusedPrivateMethod”。
我知道我可以做这样的事情:
<rule ref="rulesets/java/unusedcode.xml">
<exclude name="UnusedPrivateMethod"/>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressXPath"
value="//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']"/>
</properties>
</rule>
但在这种情况下,PMD 会跳过检查包含我的注释的类中所有方法的规则,而不仅仅是使用 @PostConstruct 注释的方法。我希望在检查代码后,我只遇到“unusedMethod”错误,并且 PMD 不会通知有关“postConstructAnnotatedMethod”的错误。
我想做这样的事情:
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressXPath"
value="//MethodDeclaration/Annotation/Name[@Image='PostConstruct']"/>
</properties>
</rule>
仅跳过带有所需注释的方法,而不是所有方法。
我也不想用很多东西污染我的代码@SuppressWarnings("PMD.UnusedPrivateMethod")
注释。
抑制 XPath 是以违规所在的节点为起点执行的,因此您可以简单地“转到方法,然后检查注释”。
例如:
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressXPath"
value="./ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']"/>
</properties>
</rule>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)