Menu

#1515 [core] Limit scope of suppressViolationXpath

New Tickets
open
nobody
XPath Rule (3)
PMD
3-Major
Feature-Request
5.4.2
All suppressions
2017-01-08
2016-08-18
No

Hi all,

I would like to be able to suppress some correctly identified but non-addressable warnings; mostly with the Law of Demeter. Certain libraries like Mokito and Guice use chain calls by design:

when(mockedClass.function()).thenReturn(someValue);

and

filter("/").through(AuthenticationFilter.class);
serve("/js/
", "/css/*").with(CustomFileServlet.class);

The lines themselves can't be annotated with @SuppressWarning("PMD.LawOfDemeter") and doing so would rapidly make the code less readable. Annotating the method or class means other violations of that type are suppressed and using //NOPMD suppresses all warnings on a line not just the Law of Demeter one.

Currently, setting the "suppressViolationXpath" property within the pmdRules.xml suppresses all violations of that rule if in the entire file if one XPath match is found. As it is already possible to suppress an entire file by annotating the top of the file it would be more useful for "suppressViolationXpath" to only suppress the lines matching the XPath. For example

//BlockStatement//PrimaryPrefix/Name[@Image='filter']

would suppress the above filter example but not the serve.

I believe the issue is with "ParametericRuleViolation.setSuppression()", which checks if any descendent of this node matches the XPath and then suppresses warnings on that node, when in fact it could just check if that node matches rather than the descendent.

If this sounds reasonable, I shall continue to try and work out how to achieve it. Otherwise, we really need another way to ignore these "by design" violations.

Kym

Discussion

  • Marcel Möhring

    Marcel Möhring - 2016-09-23

    I second this one.

    Just started using PMD and wanted to exclude public equals, hashCode and toString methods from publicMethodCommentRequirement once and for all in a config file.

    I was astonished to find out that a XPath like //MethodDeclarator[@Image='toString'] removes not only the warning for the matching path.

     
  • Andreas Dangel

    Andreas Dangel - 2017-01-08
    • summary: Limit scope of suppressViolationXpath --> [core] Limit scope of suppressViolationXpath
     

Log in to post a comment.