PMD violationSuppressXPath for all REST @GET methods?

只谈情不闲聊 提交于 2019-11-27 06:21:27

问题


I am trying to narrow down the PMD rules, how can I exclude all REST methods which are annotated with @GET from PMD checks?


回答1:


We are using for example this rules to suppress checks on REST methods for final declaration. Maybe you need similar?

<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>



回答2:


PMD provides several ways to suppress warnings: http://pmd.sourceforge.net/pmd-5.2.3/usage/suppressing.html

  • via Annotations: @SuppressWarnings("PMD.")
  • via Comments: //NOPMD ignore this
  • via Regex and XPath per Rules

You can also excluding complete files - see http://pmd.sourceforge.net/pmd-5.2.3/customizing/howtomakearuleset.html - Excluding files from a ruleset

For your case, violationSuppressXPath, this XPath expression should work:

./ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='GET']

This will go up (ancestor) from the current node (which might be inside a method) to the method declaration ("ClassOrInterfaceBodyDeclaration") and goes down the tree from there to check for the @GET annotation. However, I don't know about the performance impact.

Update:

Complete example:

<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>


来源:https://stackoverflow.com/questions/28331272/pmd-violationsuppressxpath-for-all-rest-get-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!