Merge pull request #3985 from Scrsloota:master

[java] Fix false negative problem about Enum in AvoidFieldNameMatchingMethodName #3936
This commit is contained in:
Andreas Dangel 2022-06-04 15:15:58 +02:00
commit e6669f13d5
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
3 changed files with 57 additions and 1 deletions

View File

@ -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 %}

View File

@ -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<ASTFieldDeclaration> fields = new ArrayList<>();
Set<String> methodNames = new HashSet<>();
@ -50,7 +63,6 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule {
addViolation(data, field, field.getVariableName());
}
}
return super.visit(node, data);
}
}

View File

@ -60,6 +60,48 @@ public class Bar {
<code><![CDATA[
public interface Bar {
public static final int FOO = 5;
}
]]></code>
</test-code>
<test-code>
<description>Test1 in Enum #3936</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class C {
enum E {
R;
int bar;
void bar() {} // should report a warning in this line
}
}
]]></code>
</test-code>
<test-code>
<description>Test2 in Enum #3936</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class C {
int bar;
enum E {
R;
void bar() {}
}
}
]]></code>
</test-code>
<test-code>
<description>Test3 in Enum #3936</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class C {
void bar() {}
enum E {
R;
int bar;
}
}
]]></code>
</test-code>