【发布时间】:2017-12-14 14:41:21
【问题描述】:
我有以下课程:
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") 注释污染我的代码。
【问题讨论】:
-
感谢分享@SuppressWarnings("PMD.UnusedPrivateMethod") - 这对我有用(不想碰我们的 pmd 规则集)