forked from phoedos/pmd
#1543 [java] LinguisticNaming should ignore overriden methods
Overriden methods are ignored from LinguisticNamingRule.
This commit is contained in:
@ -7,6 +7,9 @@ package net.sourceforge.pmd.lang.java.rule.codestyle;
|
||||
import static net.sourceforge.pmd.properties.PropertyFactory.booleanProperty;
|
||||
import static net.sourceforge.pmd.properties.PropertyFactory.stringListProperty;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@ -18,11 +21,11 @@ import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTResultType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTType;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractIgnoredAnnotationRule;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
|
||||
import net.sourceforge.pmd.properties.PropertyDescriptor;
|
||||
|
||||
public class LinguisticNamingRule extends AbstractJavaRule {
|
||||
public class LinguisticNamingRule extends AbstractIgnoredAnnotationRule {
|
||||
private static final PropertyDescriptor<Boolean> CHECK_BOOLEAN_METHODS =
|
||||
booleanProperty("checkBooleanMethod").defaultValue(true).desc("Check method names and types for inconsistent naming.").build();
|
||||
private static final PropertyDescriptor<Boolean> CHECK_GETTERS =
|
||||
@ -70,6 +73,11 @@ public class LinguisticNamingRule extends AbstractJavaRule {
|
||||
addRuleChainVisit(ASTLocalVariableDeclaration.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<String> defaultSuppressionAnnotations() {
|
||||
return Collections.checkedList(Arrays.asList("java.lang.Override"), String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTMethodDeclaration node, Object data) {
|
||||
String nameOfMethod = node.getMethodName();
|
||||
@ -132,10 +140,12 @@ public class LinguisticNamingRule extends AbstractJavaRule {
|
||||
}
|
||||
|
||||
private void checkSetters(ASTMethodDeclaration node, Object data, String nameOfMethod) {
|
||||
ASTResultType resultType = node.getResultType();
|
||||
if (hasPrefix(nameOfMethod, "set") && !resultType.isVoid()) {
|
||||
addViolationWithMessage(data, node, "Linguistics Antipattern - The setter ''{0}'' should not return any type except void linguistically",
|
||||
new Object[] { nameOfMethod });
|
||||
if (!hasIgnoredAnnotation(node)) {
|
||||
ASTResultType resultType = node.getResultType();
|
||||
if (hasPrefix(nameOfMethod, "set") && !resultType.isVoid()) {
|
||||
addViolationWithMessage(data, node, "Linguistics Antipattern - The setter ''{0}'' should not return any type except void linguistically",
|
||||
new Object[] { nameOfMethod });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -549,6 +549,29 @@ public class AtomicBooleanFN {
|
||||
AtomicBoolean willX = new AtomicBoolean(true);
|
||||
AtomicBoolean shouldX = new AtomicBoolean(true);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#1543 [java] LinguisticNaming should ignore overriden methods</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
class Foo {
|
||||
private int value;
|
||||
|
||||
public int setValue(int value) {
|
||||
this.value = value;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
public class Bar extends Foo {
|
||||
private int value;
|
||||
|
||||
@Override
|
||||
public int setValue(int value) {
|
||||
this.value = value;
|
||||
return value;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
Reference in New Issue
Block a user