我正在尝试缩小PMD规则,如何从PMD检查中排除所有使用@GET注释的REST方法?
答案 0 :(得分:3)
PMD提供了几种抑制警告的方法:http://pmd.sourceforge.net/pmd-5.2.3/usage/suppressing.html
您也可以排除完整文件 - 请参阅http://pmd.sourceforge.net/pmd-5.2.3/customizing/howtomakearuleset.html - 从规则集中排除文件
对于您的情况violationSuppressXPath
,此XPath表达式应该起作用:
./ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='GET']
这将从当前节点(可能在方法内)上升到(祖先)到方法声明(" ClassOrInterfaceBodyDeclaration")并从那里向下看树以检查@GET注释。但是,我不了解性能影响。
<强>更新强>
完整示例:
<rule ref="rulesets/java/optimizations.xml/MethodArgumentCouldBeFinal">
<properties>
<!-- Ignore Rest resources -->
<property name="violationSuppressXPath" value="
./ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name
[@Image='GET' or @Image='POST' or @Image='PUT' or @Image='DELETE']" />
</properties>
</rule>
答案 1 :(得分:2)
我们正在使用此规则来禁止检查REST方法以进行最终声明。也许你需要类似的?
<rule ref="rulesets/java/optimizations.xml/MethodArgumentCouldBeFinal">
<properties>
<!-- Ignore Rest resources -->
<property name="violationSuppressXPath" value="
//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='GET'] |
//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='POST']|
//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='PUT'] |
//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation//Name[@Image='DELETE']" />
</properties>
</rule>