Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to exclude some rules from PMD

I'm just trying to add PMD to my Spring Boot project but I'm facing some issues trying to exclude some rules from category/java/errorprone.xml ruleset.

I'm using the gradle plugin as follows:

pmd {
    ruleSetConfig = resources.text.fromFile("config/pmd/ruleset.xml")
}

and this is the content of my custom ruleset file:

<?xml version="1.0"?>
<ruleset>
    <rule ref="category/java/bestpractices.xml"></rule>
    <rule ref="category/java/codestyle.xml">
        <exclude name="MethodArgumentCouldBeFinal"/>
        <exclude name="LocalVariableCouldBeFinal"/>
        <exclude name="ControlStatementBraces"/>
        <exclude name="OnlyOneReturn"/>
        <exclude name="ConfusingTernary"/>
        <exclude name="AtLeastOneConstructor"/>
        <exclude name="AvoidFinalLocalVariable"/>
        <exclude name="ShortVariable"/>
        <exclude name="LongVariable"/>
        <exclude name="CommentDefaultAccessModifier"/>
        <exclude name="DefaultPackage"/>
        <exclude name="PrematureDeclaration"/>
    </rule>
    <rule ref="category/java/design.xml">
        <exclude name="LawOfDemeter"/>
        <exclude name="NcssCount"/>
        <exclude name="CyclomaticComplexity"/>
        <exclude name="UseUtilityClass"/>
        <exclude name="AvoidCatchingGenericException"/>
        <exclude name="NPathComplexity"/>
        <exclude name="AvoidRethrowingException"/>
        <exclude name="DataClass"/>
        <exclude name="AvoidThrowingRawExceptionTypes"/>
    </rule>
    <rule ref="category/java/documentation.xml">
        <exclude name="CommentRequired"/>
        <exclude name="UncommentedEmptyConstructor"/>
        <exclude name="CommentSize"/>
    </rule>
    <rule ref="category/java/errorprone.xml">
        <exclude name="BeanMembersShouldSerialize"/>
        <exclude name="DataflowAnomalyAnalysis"/>
        <exclude name="AssignmentInOperand"/>
    </rule>
    <rule ref="category/java/multithreading.xml">
    </rule>
    <rule ref="category/java/performance.xml">
        <exclude name="AvoidInstantiatingObjectsInLoops"/>
    </rule>
    <rule ref="category/java/security.xml">
    </rule>
</ruleset>

As you can see there, I'm excluding both BeanMembersShouldSerializeand DataflowAnomalyAnalysis but I'm still getting these errors in the PMD report:

Found non-transient, non-static member. Please mark as transient or provide accessors.

`Found 'DU'-anomaly for variable 'userEntity' (lines '28'-'38').``

I'm using PMD version 6.10.0 (December 9th 2018).

Any help would be really appreciated.

Regards

like image 473
Victor Escobar Avatar asked Dec 09 '18 19:12

Victor Escobar


People also ask

What are PMD rules?

Conceptually, PMD rules work by matching a “pattern” against the AST of a file. Rules explore the AST and find nodes that satisfy some conditions that are characteristic of the specific thing the rule is trying to flag. Rules then report a violation on these nodes.


1 Answers

Gradle defaults to using the basic and braces rulesets up to Gradle 5. Gradle 5 defaults to using the errorprone category.

You need to clear out these defaults if you don't want them by doing:

pmd {
    ruleSets = [] 
}

https://docs.gradle.org/current/dsl/org.gradle.api.plugins.quality.Pmd.html#org.gradle.api.plugins.quality.Pmd:ruleSets

like image 137
Johnco Avatar answered Sep 19 '22 19:09

Johnco