Menu

violation suppression scope

2016-07-07
2016-08-18
  • Kym Eden-Jones

    Kym Eden-Jones - 2016-07-07

    Hi all,

    I'm tring to find a way to remove some correctly identitifed 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);
    

    for example.

    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 violations in other places are suppressed and //NOPMD suppresses all warnings on a line not just the Law of Demeter one.

    Is there no way to make suppressViolationXpath match only the scope of the element identified. For example

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

    should capture just the offending line, not the whole class.

    (Also as a foot note: from the documentation it's really not obvious that suppressViolationXpath affects the whole class if any lines are matched)

     
  • Kym Eden-Jones

    Kym Eden-Jones - 2016-08-18

    The problem is with ParametericRuleViolation.setSuppression()

    if (!suppressed) {  // XPath
                String xpath = rule.getProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR);
                if (xpath != null) {
                    suppressed = node.hasDescendantMatchingXPath(xpath);
                }
            }
    

    should surely be

    if (!suppressed) {  // XPath
                String xpath = rule.getProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR);
                if (xpath != null) {
                    suppressed = node.matchesXPath(xpath);
                }
            }
    

    but I'm currently unable to get the git project to build due to all sorts of project and dependency errors in Ecplise so I haven't been able to constrct Node.matchesXPath().

     

    Last edit: Kym Eden-Jones 2016-08-18

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.