我们有一个项目,由PMD检查是否违反例如未使用的私有方法。我们的问题是,我们不知道是否可以忽略用@PostConstruct
注释的私有方法。
该规则定义如下:
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod"/>
编辑:
我的目标是定义一次以忽略注释方法。我想防止在每种方法上都写@SupressWarnings
。
在 HairyFotr 的提示和建议下,我能够将我的规则集配置为忽略private
方法与 @PostConstruct
.
我必须使用的规则是:
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressXPath"
value="//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']" />
</properties>
</rule>
只要至少存在一个@PostConstruct注释,moz987 的答案就会抑制文件中所有未使用的 UnusedPrivateMethod 违规行为。如果只想禁止来自具有@PostConstruct注释的方法的冲突,并保留来自没有注释的方法的冲突,则必须在 XPath 前面加上 ancestor::
而不是 //
。
注意:下面的示例使用 PMD 6.0.0 的新规则引用。
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
<properties>
<property name="violationSuppressXPath"
value="ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']" />
</properties>
</rule>