From 1a62ddffad8bf4e9beb30205a40f45ec22bb2cff Mon Sep 17 00:00:00 2001 From: Scrsloota <1912125562@qq.com> Date: Sat, 28 May 2022 00:17:11 +0800 Subject: [PATCH 1/4] fix AvoidFieldNameMatchingMethodName without enum --- .../AvoidFieldNameMatchingMethodNameRule.java | 54 +++++++++++-------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java index a84a2b8b3d..9fd52b984a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java @@ -13,8 +13,11 @@ import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTEnumBody; +import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; +import net.sourceforge.pmd.lang.java.ast.JavaNode; import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule { @@ -28,29 +31,36 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule { } @Override - public Object visit(ASTClassOrInterfaceBody node, Object data) { - int n = node.getNumChildren(); - List fields = new ArrayList<>(); - Set 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()); - } - } + public Object visit(ASTEnumDeclaration node, Object data) { return super.visit(node, data); } + @Override + public Object visit(JavaNode node, Object data) { + if (node instanceof ASTClassOrInterfaceBody || node instanceof ASTEnumBody) { + int n = node.getNumChildren(); + List fields = new ArrayList<>(); + Set 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); + } } From 7261b7fd10842fc15c3aab73de1ea8d0c7944dcc Mon Sep 17 00:00:00 2001 From: Scrsloota <1912125562@qq.com> Date: Sat, 28 May 2022 00:17:32 +0800 Subject: [PATCH 2/4] add test cases --- .../xml/AvoidFieldNameMatchingMethodName.xml | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml index 138d598230..ad0170102a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml @@ -60,6 +60,48 @@ public class Bar { + + + + Test1 in Enum + 1 + + + + + Test2 in Enum + 0 + + + + + Test3 in Enum + 0 + From 051951e852f4f7753ffc51f0f3cc005b0ba9a2da Mon Sep 17 00:00:00 2001 From: Scrsloota <1912125562@qq.com> Date: Thu, 2 Jun 2022 23:43:31 +0800 Subject: [PATCH 3/4] change to avoid instanceof checks --- .../AvoidFieldNameMatchingMethodNameRule.java | 56 ++++++++++--------- .../xml/AvoidFieldNameMatchingMethodName.xml | 6 +- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java index 9fd52b984a..5e5dd5de17 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/errorprone/AvoidFieldNameMatchingMethodNameRule.java @@ -14,7 +14,6 @@ import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTEnumBody; -import net.sourceforge.pmd.lang.java.ast.ASTEnumDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; import net.sourceforge.pmd.lang.java.ast.JavaNode; @@ -31,36 +30,39 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule { } @Override - public Object visit(ASTEnumDeclaration node, Object data) { + public Object visit(ASTClassOrInterfaceBody node, Object data) { + handleClassOrEnum(node, data); return super.visit(node, data); } @Override - public Object visit(JavaNode node, Object data) { - if (node instanceof ASTClassOrInterfaceBody || node instanceof ASTEnumBody) { - int n = node.getNumChildren(); - List fields = new ArrayList<>(); - Set 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); - } + public Object visit(ASTEnumBody node, Object data) { + handleClassOrEnum(node, data); return super.visit(node, data); } + + private void handleClassOrEnum(JavaNode node, Object data) { + int n = node.getNumChildren(); + List fields = new ArrayList<>(); + Set 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()); + } + } + } + } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml index ad0170102a..4cab17b54a 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/errorprone/xml/AvoidFieldNameMatchingMethodName.xml @@ -65,7 +65,7 @@ public interface Bar { - Test1 in Enum + Test1 in Enum #3936 1 - Test2 in Enum + Test2 in Enum #3936 0 - Test3 in Enum + Test3 in Enum #3936 0 Date: Sat, 4 Jun 2022 15:14:35 +0200 Subject: [PATCH 4/4] [doc] Update release notes (#3936, #3985) --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 1eb0bab3e3..fe77034c8c 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -16,11 +16,13 @@ This is a {{ site.pmd.release_type }} release. ### Fixed Issues * java-errorprone + * [#3936](https://github.com/pmd/pmd/issues/3936): \[java] AvoidFieldNameMatchingMethodName should consider enum class * [#3937](https://github.com/pmd/pmd/issues/3937): \[java] AvoidDuplicateLiterals - uncompilable test cases ### API Changes ### External Contributions +* [#3985](https://github.com/pmd/pmd/pull/3985): \[java] Fix false negative problem about Enum in AvoidFieldNameMatchingMethodName #3936 - [@Scrsloota](https://github.com/Scrsloota) * [#3993](https://github.com/pmd/pmd/pull/3993): \[java] AvoidDuplicateLiterals - Add the method "buz" definition to test cases - [@dalizi007](https://github.com/dalizi007) {% endtocmaker %}