[java] Update rule MethodWithSameNameAsEnclosingClass

Converted the rule into a simle XPath rule
This commit is contained in:
Andreas Dangel committed 2021-11-29 09:37:27 +01:00
1 parent f36b99fbd7
commit dd8b25802d
5 files changed
+12 -28

No files matched your search

+1 -1
View File
@@ -233,7 +233,7 @@
<rule ref="category/java/errorprone.xml/JUnitSpelling"/>
<rule ref="category/java/errorprone.xml/JUnitStaticSuite"/>
<rule ref="category/java/errorprone.xml/JumbledIncrementer"/>
<!-- <rule ref="category/java/errorprone.xml/MethodWithSameNameAsEnclosingClass"/> -->
<rule ref="category/java/errorprone.xml/MethodWithSameNameAsEnclosingClass"/>
<rule ref="category/java/errorprone.xml/MisplacedNullCheck"/>
<rule ref="category/java/errorprone.xml/MissingSerialVersionUID"/>
<rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"/>
@@ -1,25 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.errorprone;
import java.util.List;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
public class MethodWithSameNameAsEnclosingClassRule extends AbstractJavaRule {
@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
List<ASTMethodDeclarator> methods = node.findDescendantsOfType(ASTMethodDeclarator.class);
for (ASTMethodDeclarator m : methods) {
if (m.hasImageEqualTo(node.getImage())) {
addViolation(data, m);
}
}
return super.visit(node, data);
}
}
@@ -2180,12 +2180,21 @@ public class Foo extends TestCase {
language="java"
since="1.5"
message="Classes should not have non-constructor methods with the same name as the class"
class="net.sourceforge.pmd.lang.java.rule.errorprone.MethodWithSameNameAsEnclosingClassRule"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#methodwithsamenameasenclosingclass">
<description>
Non-constructor methods should not have the same name as the enclosing class.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[@Name = ancestor::ClassOrInterfaceDeclaration/@SimpleName]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class MyClass {
@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.errorprone;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class MethodWithSameNameAsEnclosingClassTest extends PmdRuleTst {
// no additional unit tests
}
@@ -7,6 +7,7 @@
<test-code>
<description>bad</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<code><![CDATA[
public class Foo {
int Foo(double x) {}