yegor256 / qulice

Quality Police for Java projects: aggregator of Checkstyle and PMD
https://www.qulice.com
Other
301 stars 112 forks source link

PMD.AvoidAccessToStaticMembersViaThis check works incorrectly #1197

Closed volodya-lombrozo closed 8 months ago

volodya-lombrozo commented 9 months ago

I have the following Java code in HasMethod.java:

@Override
public boolean matchesSafely(final String item) {
    final XMLDocument document = new XMLDocument(item);
    return this.checks().stream().map(document::xpath).noneMatch(List::isEmpty); // 113 line is here
}

When I run mvn qulice:check -Pqulice with version 0.22.1, I'm getting the following error:

[INFO] PMD: src/test/java/org/eolang/jeo/representation/directives/HasMethod.java[113-113]: Static members should be accessed in a static way [CLASS_NAME.FIELD_NAME], not via instance reference. (AvoidAccessToStaticMembersViaThis)

It's rather strange because the checks() is an instance method, not a static member:

/**
 * Checks.
 * @return List of XPaths to check.
 */
private List<String> checks() {
    return Stream.concat(
        Stream.concat(
            this.definition(),
            this.parameters()
        ),
        Stream.concat(
            this.trycatch(),
            Stream.concat(
                this.instructions(),
                this.labels()
            )
        )
    ).collect(Collectors.toList());
}

I've tried to find the description of the AvoidAccessToStaticMembersViaThis check in PMD and failed. Maybe this check somehow related to qulice?

volodya-lombrozo commented 9 months ago

BTW, using @SuppressWarnings("PMD.AvoidAccessToStaticMembersViaThis") helps to avoid the qulice complaint.

volodya-lombrozo commented 9 months ago

@yegor256 Could you help here, please?

k3p7i3 commented 8 months ago

@volodya-lombrozo it seems i found the cause of the problem - in the end of HasMethod.java there is a nested static class with static Stream<String> checks() method, and qulice take into account all method declarations named as checks(). I'll see what i can do about it.

    private static final class HasLabel {

        /**
         * Checks of label.
         * @param root Root Method XPath.
         * @return List of XPaths to check.
         */
        static Stream<String> checks(final String root) {
            return Stream.of(
                String.format(
                    "%s/o[@base='seq']/o[@base='tuple']/o[@base='label' and @data='bytes']/@data",
                    root
                )
            );
        }
    }
pnatashap commented 8 months ago

Rule AvoidAccessToStaticMembersViaThis get all methods from the file, not from the class body. It is the reason for bug. Will ask in pmd how to avoid warning messages and will fix the rule to get only current class methods