我正在尝试缩小 PMD 规则的范围,如何从 PMD 检查中排除所有用 @GET 注释的 REST 方法?
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']
Run Code Online (Sandbox Code Playgroud)
这将从当前节点(可能在方法内部)上升(祖先)到方法声明(“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>
Run Code Online (Sandbox Code Playgroud)
小智 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1876 次 |
| 最近记录: |