forked from phoedos/pmd
Merge pull request #3985 from Scrsloota:master
[java] Fix false negative problem about Enum in AvoidFieldNameMatchingMethodName #3936
This commit is contained in:
@@ -16,11 +16,13 @@ This is a {{ site.pmd.release_type }} release.
|
|||||||
|
|
||||||
### Fixed Issues
|
### Fixed Issues
|
||||||
* java-errorprone
|
* 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
|
* [#3937](https://github.com/pmd/pmd/issues/3937): \[java] AvoidDuplicateLiterals - uncompilable test cases
|
||||||
|
|
||||||
### API Changes
|
### API Changes
|
||||||
|
|
||||||
### External Contributions
|
### 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)
|
* [#3993](https://github.com/pmd/pmd/pull/3993): \[java] AvoidDuplicateLiterals - Add the method "buz" definition to test cases - [@dalizi007](https://github.com/dalizi007)
|
||||||
|
|
||||||
{% endtocmaker %}
|
{% endtocmaker %}
|
||||||
|
@@ -13,8 +13,10 @@ import java.util.Set;
|
|||||||
import net.sourceforge.pmd.lang.ast.Node;
|
import net.sourceforge.pmd.lang.ast.Node;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody;
|
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
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.ASTFieldDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||||
|
import net.sourceforge.pmd.lang.java.ast.JavaNode;
|
||||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||||
|
|
||||||
public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule {
|
public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule {
|
||||||
@@ -29,6 +31,17 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visit(ASTClassOrInterfaceBody node, Object data) {
|
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();
|
int n = node.getNumChildren();
|
||||||
List<ASTFieldDeclaration> fields = new ArrayList<>();
|
List<ASTFieldDeclaration> fields = new ArrayList<>();
|
||||||
Set<String> methodNames = new HashSet<>();
|
Set<String> methodNames = new HashSet<>();
|
||||||
@@ -50,7 +63,6 @@ public class AvoidFieldNameMatchingMethodNameRule extends AbstractJavaRule {
|
|||||||
addViolation(data, field, field.getVariableName());
|
addViolation(data, field, field.getVariableName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return super.visit(node, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -60,6 +60,48 @@ public class Bar {
|
|||||||
<code><![CDATA[
|
<code><![CDATA[
|
||||||
public interface Bar {
|
public interface Bar {
|
||||||
public static final int FOO = 5;
|
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>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user