所有 REST @GET 方法的 PMD violationSuppressXPath?

PMD violationSuppressXPath for all REST @GET methods?

我正在尝试缩小 PMD 规则的范围,如何从 PMD 检查中排除所有用 @GET 注释的 REST 方法?

PMD 提供了几种抑制警告的方法:http://pmd.sourceforge.net/pmd-5.2.3/usage/suppressing.html

  • 通过注释:@SuppressWarnings("PMD.")
  • 通过评论://NOPMD 忽略这个
  • 根据规则通过正则表达式和 XPath

您还可以排除完整文件 - 请参阅 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>

例如,我们正在使用此规则来禁止检查 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>