Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMD.AvoidAccessToStaticMembersViaThis check works incorrectly #1197

Closed
volodya-lombrozo opened this issue Mar 6, 2024 · 4 comments · Fixed by #1229
Closed

PMD.AvoidAccessToStaticMembersViaThis check works incorrectly #1197

volodya-lombrozo opened this issue Mar 6, 2024 · 4 comments · Fixed by #1229
Labels

Comments

@volodya-lombrozo
Copy link

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
Copy link
Author

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

@volodya-lombrozo
Copy link
Author

@yegor256 Could you help here, please?

@yegor256 yegor256 added the bug label Mar 6, 2024
@k3p7i3
Copy link
Contributor

k3p7i3 commented Mar 10, 2024

@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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants