Refine definition of a getter

This commit is contained in:
Clément Fournier committed 2021-03-28 16:49:39 +02:00
1 parent d6d316eba5
commit 93c04c4b1f
3 files changed
+53 -5

No files matched your search

@@ -8,7 +8,9 @@ import org.apache.commons.lang3.mutable.MutableInt;
import net.sourceforge.pmd.lang.java.ast.ASTFieldAccess;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTThisExpression;
import net.sourceforge.pmd.lang.java.ast.JavaVisitorBase;
import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil;
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
@@ -38,8 +40,9 @@ public class AtfdBaseVisitor extends JavaVisitorBase<MutableInt, Void> {
}
private boolean isForeignMethod(ASTMethodCall node) {
return node.getMethodName().startsWith("set")
|| node.getMethodName().startsWith("get");
return JavaRuleUtil.isGetterOrSetterCall(node) // getter or setter
&& node.getQualifier() != null // not called on this
&& !(node.getQualifier() instanceof ASTThisExpression);
}
}
@@ -248,6 +248,30 @@ public final class JavaRuleUtil {
|| "_".equals(name); // before java 9 it's ok
}
/**
* Returns true if the string has the given word as a strict prefix.
* There needs to be a camelcase word boundary after the prefix.
*
* <code>
* startsWithCamelCaseWord("getter", "get") == false
* startsWithCamelCaseWord("get", "get") == false
* startsWithCamelCaseWord("getX", "get") == true
* </code>
*
* @param camelCaseString A string
* @param prefixWord A prefix
*/
static boolean startsWithCamelCaseWord(String camelCaseString, String prefixWord) {
return camelCaseString.startsWith(prefixWord)
&& camelCaseString.length() > prefixWord.length()
&& Character.isUpperCase(camelCaseString.charAt(prefixWord.length()));
}
public static boolean isGetterOrSetterCall(ASTMethodCall call) {
return call.getArguments().size() == 0 && startsWithCamelCaseWord(call.getMethodName(), "get")
|| call.getArguments().size() > 0 && startsWithCamelCaseWord(call.getMethodName(), "set");
}
public static boolean isGetterOrSetter(ASTMethodDeclaration node) {
return isGetter(node) || isSetter(node);
@@ -261,9 +285,9 @@ public final class JavaRuleUtil {
}
ASTAnyTypeDeclaration enclosing = node.getEnclosingType();
if (node.getName().startsWith("get")) {
if (startsWithCamelCaseWord(node.getName(), "get")) {
return hasField(enclosing, node.getName().substring(3));
} else if (node.getName().startsWith("is")) {
} else if (startsWithCamelCaseWord(node.getName(), "is")) {
return hasField(enclosing, node.getName().substring(2));
}
@@ -279,7 +303,7 @@ public final class JavaRuleUtil {
ASTAnyTypeDeclaration enclosing = node.getEnclosingType();
if (node.getName().startsWith("set")) {
if (startsWithCamelCaseWord(node.getName(), "set")) {
return hasField(enclosing, node.getName().substring(3));
}
@@ -0,0 +1,21 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.internal;
import static net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil.startsWithCamelCaseWord;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class JavaRuleUtilTest {
@Test
public void testCamelCaseWords() {
assertFalse(startsWithCamelCaseWord("getter", "get"));
assertFalse(startsWithCamelCaseWord("get", "get"));
assertTrue(startsWithCamelCaseWord("getX", "get"));
}
}