GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] AvoidFieldNameMatchingMethodName should consider enum class · Issue #3936 · pmd/pmd

[java] AvoidFieldNameMatchingMethodName should consider enum class #3936

New issue
Copy link
New issue
Copy link
Closed
#3985
Closed
[java] AvoidFieldNameMatchingMethodName should consider enum class#3936
#3985
Copy link
Assignees
Scrsloota
Labels
a:false-negativePMD doesn't flag a problematic piece of codegood first issueA great starting point for new contributors
Milestone
6.47.0

Description

@ghost
ghost
opened on Apr 23, 2022

Affects PMD Version:6.42.0

Rule:

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_errorprone.html#avoidfieldnamematchingmethodname

Description:
Hi, I found a false negative about the rule AvoidFieldNameMatchingMethodName. PMD should have reported a warning in line 5. I think this bug is caused by enum class here. Thanks for your consideration.

Code Sample demonstrating the issue:

public class C {
  enum E {
      R;
      int bar;
      void bar() {}  // should report a warning in this line
  }
}

Expected outcome:

PMD should report a violation at line 5, but doesn't. This is a false-negative.

Running PMD through: [Maven]

Activity

  1. ghost added
    a:false-negativePMD doesn't flag a problematic piece of code
    on Apr 23, 2022
  2. adangel
    changed the title [-]AvoidFieldNameMatchingMethodName should consider enum class[/-] [+][java] AvoidFieldNameMatchingMethodName should consider enum class[/+] on Apr 29, 2022
  3. adangel
    added
    good first issueA great starting point for new contributors
    on Apr 29, 2022
  4. Scrsloota commented on May 27, 2022

    @Scrsloota
    Scrsloota
    on May 27, 2022
    Contributor
    More actions

    Please assign this to me! 😊

  5. Scrsloota commented on May 27, 2022

    @Scrsloota
    Scrsloota
    on May 27, 2022
    Contributor
    More actions

    I used the rule designer to inspect the AST of the following codes:

    public class C {
      class E {
          int bar;
          void bar() {}  // should report a warning in this line
      }
    }
    public class C {
      enum E {
          R;
          int bar;
          void bar() {}  // should report a warning in this line
      }
    }

    The result shows that:

    • class E is a node of ClassOrInterfaceDeclaration and the node of the whole braces is ClassOrInterfaceBody.
    • enum E is a node of EnumDeclaration and the node of the whole braces is EnumBody.
    • R is shown as a EnumConstant, and all other things inside are the same (FieldDeclaration or MethodDeclaration ).

    So, first I should make the Enum class be able in AvoidFieldNameMatchingMethodNameRule, I add following method to catch the EnumDeclaration node:

        @Override
        public Object visit(ASTEnumDeclaration node, Object data) {
            return super.visit(node, data);
        }

    The analytical method for ClassOrInterfaceBody and EnumBody are the same, so in order to avoid duplicate codes, I modified the original visit(ASTClassOrInterfaceBody node, Object data) method as follow to fix the problem:

        @Override
        public Object visit(JavaNode node, Object data) {
            if (node instanceof ASTClassOrInterfaceBody || node instanceof ASTEnumBody) {
                int n = node.getNumChildren();
                List<ASTFieldDeclaration> fields = new ArrayList<>();
                Set<String> methodNames = new HashSet<>();
                for (int i = 0; i < n; i++) {
                    Node child = node.getChild(i);
                    if (child.getNumChildren() == 0) {
                        continue;
                    }
                    child = child.getChild(child.getNumChildren() - 1);
                    if (child instanceof ASTFieldDeclaration) {
                        fields.add((ASTFieldDeclaration) child);
                    } else if (child instanceof ASTMethodDeclaration) {
                        methodNames.add(((ASTMethodDeclaration) child).getName().toLowerCase(Locale.ROOT));
                    }
                }
                for (ASTFieldDeclaration field : fields) {
                    String varName = field.getVariableName().toLowerCase(Locale.ROOT);
                    if (methodNames.contains(varName)) {
                        addViolation(data, field, field.getVariableName());
                    }
                }
                return super.visit(node, data);
            }
            return super.visit(node, data);
        }
  6. adangel
    assigned
    Scrsloota
    on May 27, 2022
  7. adangel
    modified the milestone: 6.47.0 on May 28, 2022
  8. adangel
    added 2 commits that reference this issue on Jun 4, 2022

    [doc] Update release notes (#3936, #3985)

    62d27c0

    Merge pull request #3985 from Scrsloota:master

    e6669f1
  9. adangel
    closed this as completedin #3985on Jun 4, 2022
  10. rdesgroppes
    added 2 commits that reference this issue on Apr 26, 2023

    Bump PMD version to last planned release (6.55.0)

    9ed8991

    Bump PMD version to last planned release (6.55.0)

    23e1897
  11. illicitonion
    added a commit that references this issue on Apr 26, 2023

    Bump PMD version to last planned release (6.55.0) (#178)

    d99cc64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @Scrsloota
    Scrsloota

Labels

a:false-negativePMD doesn't flag a problematic piece of codegood first issueA great starting point for new contributors

Type

No type

Projects

No projects

    Milestone

    • 6.47.0

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.