How to tell PMD to ignore @PostConstruct methods for unused code


we have a project that PMD checks for violations, for example. unused private methods. Our problem is that we don’t know whether private methods that are annotated using can be ignored @PostConstruct.

A rule is defined as follows:

<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod"/>

Edit:

My goal is to define it once to ignore annotated methods. I would like to prevent writing @SupressWarningsfor each method.

+4
source share
3 answers

HairyFotr , private @PostConstruct.

, :

<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateMethod">
    <properties> 
        <property name="violationSuppressXPath" 
            value="//ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']" />
    </properties>
</rule>   
+7

moz987 answer UnusedPrivateMethod , @PostConstruct. , @PostConstruct, , XPath ancestor:: //.

. PMD 6.0.0.

  <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod">
    <properties>
      <property name="violationSuppressXPath"
                value="ancestor::ClassOrInterfaceBodyDeclaration/Annotation/MarkerAnnotation/Name[@Image='PostConstruct']" />
    </properties>
  </rule>
0

Since your question doesn’t show any effort, my answer will not show either either (I was hoping to post the “let me google what for you” link here, but unfortunately it’s blocked here, but basically go to google.com, enter pmd ignore unusedand click I'm feeling lucky...).

@SuppressWarnings("unused")

-2
source

Source: https://habr.com/ru/post/1625142/


All Articles