#1343 MethodNamingConventions for overrided methods

This commit is contained in:
Andreas Dangel
2015-05-15 15:33:40 +02:00
parent cae16d39d7
commit 3c316c2018
3 changed files with 32 additions and 0 deletions

View File

@ -3,9 +3,14 @@
*/
package net.sourceforge.pmd.lang.java.rule.naming;
import java.util.List;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
@ -30,6 +35,10 @@ public class MethodNamingConventionsRule extends AbstractJavaRule {
return data;
}
if (isOverriddenMethod(node)) {
return data;
}
String methodName = node.getImage();
if (Character.isUpperCase(methodName.charAt(0))) {
@ -41,4 +50,15 @@ public class MethodNamingConventionsRule extends AbstractJavaRule {
return data;
}
private boolean isOverriddenMethod(ASTMethodDeclarator node) {
ASTClassOrInterfaceBodyDeclaration declaration = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
List<ASTMarkerAnnotation> annotations = declaration.findDescendantsOfType(ASTMarkerAnnotation.class);
for (ASTMarkerAnnotation ann : annotations) {
ASTName name = ann.getFirstChildOfType(ASTName.class);
if (name != null && name.hasImageEqualTo("Override")) {
return true;
}
}
return false;
}
}

View File

@ -51,6 +51,17 @@ public class Foo {
<code><![CDATA[
public class Foo {
protected final native void __surfunc__(float[] data);
}
]]></code>
</test-code>
<test-code>
<description>#1343 MethodNamingConventions for overrided methods</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class MethodNamingConventions implements SomeInterface {
@Override
public void _foo() {
}
}
]]></code>
</test-code>

View File

@ -14,6 +14,7 @@
* [#1335](https://sourceforge.net/p/pmd/bugs/1335/): GuardLogStatementJavaUtil should not apply to SLF4J Logger
* [#1342](https://sourceforge.net/p/pmd/bugs/1342/): UseConcurrentHashMap false positive (with documentation example)
* [#1343](https://sourceforge.net/p/pmd/bugs/1343/): MethodNamingConventions for overrided methods
* [#1345](https://sourceforge.net/p/pmd/bugs/1345/): UseCollectionIsEmpty throws NullPointerException
* [#1353](https://sourceforge.net/p/pmd/bugs/1353/): False positive "Only One Return" with lambda