2

I've got ViewState POJO classes which have constructors with many parameters. Problem is that PMD is throwing ExcessiveParameterList violation on them.

Now I'm trying to suppress this violation for all classes which end with ViewState.java (e.g. in DashboardViewState.java). I've added this to my rules-pmd.xml:

<rule ref="category/java/design.xml/ExcessiveParameterList">
    <properties>
        <!--Ignore ExcessiveParameterList on ViewState classes -->
        <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration['*ViewState.java']"/>
    </properties>
</rule>

Problem is that this will suppress all violations against ExcessiveParameterList no matter in which class. What am I doing wrong?

Linus Fernandes
  • 498
  • 5
  • 30
mbo
  • 4,611
  • 2
  • 34
  • 54
  • Maybe this would help: https://pmd.github.io/latest/pmd_userdocs_making_rulesets.html#filtering-the-processed-files - use `` instead of `` – Amongalen Jun 07 '19 at 11:02
  • But this is just to completely exclude classes from all rules, or not? I just want to suppress one rule. – mbo Jun 07 '19 at 11:05

1 Answers1

5

this is a duplicate of this question, though since nobody upvoted my answer I can't flag this as a duplicate.

See https://stackoverflow.com/a/56460327/6245827 for details about why your expression suppresses all violations of the rule.

The solution here is to test the @Image attribute of the ClassOrInterfaceDeclaration, and not using //, but rather an ancestor check:

./ancestor::ClassOrInterfaceDeclaration[contains(@Image, 'ViewState')]

XPath 1.0 doesn't support regex, so you're limited to doing a contains check like here, or mimicking an ends-with function with substring, like explained in this answer

oowekyala
  • 7,518
  • 1
  • 15
  • 20
  • 1
    Seems to be the same problem, but this question is way easier to understand and to find for others. I've looked around quite a bit to find an answer before creating the question – mbo Jun 12 '19 at 07:31
  • @Marius you may be right, this question has a better title – oowekyala Jun 12 '19 at 16:23
  • 1
    PMD provides for XPath 1.0 a `matches` function to support regex. You can write the example this way: `./ancestor::ClassOrInterfaceDeclaration[matches(@Image, '^.*ViewState$')]` – adangel Jun 14 '19 at 12:54