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 %} 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..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 @@ -13,8 +13,10 @@ 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.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 { @@ -29,6 +31,17 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule { @Override public Object visit(ASTClassOrInterfaceBody node, Object data) { + handleClassOrEnum(node, data); + return super.visit(node, data); + } + + @Override + 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<>(); @@ -50,7 +63,6 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule { addViolation(data, field, field.getVariableName()); } } - return super.visit(node, data); } } 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..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 @@ -60,6 +60,48 @@ public class Bar { + + + + Test1 in Enum #3936 + 1 + + + + + Test2 in Enum #3936 + 0 + + + + + Test3 in Enum #3936 + 0 +