PMD violationSuppressXPath for all REST @GET methods?

前端 未结 2 774
南笙
南笙 2020-12-10 19:29

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

相关标签:
2条回答
  • 2020-12-10 19:46

    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>
    
    0 讨论(0)
  • 2020-12-10 19:49

    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>
    
    0 讨论(0)
提交回复
热议问题