diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java index d810a60175..9ca1e6985a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java @@ -7,7 +7,7 @@ package net.sourceforge.pmd.lang.java.oom.signature; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.regex.Pattern; +import java.util.Set; import net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration; import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; @@ -80,7 +80,7 @@ public final class OperationSignature extends Signature { GETTER_OR_SETTER, CONSTRUCTOR, METHOD, STATIC; - private static final Pattern GETTER_OR_SETTER_NAME_PATTERN = Pattern.compile("(?:get|set|is)\\w*"); + // private static final Pattern GETTER_OR_SETTER_NAME_PATTERN = Pattern.compile("(?:get|set|is)\\w*"); public static Role get(ASTMethodOrConstructorDeclaration node) { @@ -100,10 +100,6 @@ public final class OperationSignature extends Signature { private static boolean isGetterOrSetter(ASTMethodDeclaration node) { - String name = node.getName(); - if (GETTER_OR_SETTER_NAME_PATTERN.matcher(name).matches()) { - return true; - } ClassScope scope = node.getScope().getEnclosingScope(ClassScope.class); @@ -131,8 +127,15 @@ public final class OperationSignature extends Signature { || node.getFirstDescendantOfType(ASTResultType.class).isVoid()) { return false; } - return fieldNames.containsKey(node.getName()); + if (node.getName().startsWith("get")) { + return containsIgnoreCase(fieldNames.keySet(), node.getName().substring(3)); + } else if (node.getName().startsWith("is")) { + return containsIgnoreCase(fieldNames.keySet(), node.getName().substring(2)); + } + + + return fieldNames.containsKey(node.getName()); } @@ -144,7 +147,20 @@ public final class OperationSignature extends Signature { return false; } + if (node.getName().startsWith("set")) { + return containsIgnoreCase(fieldNames.keySet(), node.getName().substring(3)); + } + return fieldNames.containsKey(node.getName()); } + + private static boolean containsIgnoreCase(Set set, String str) { + for (String s : set) { + if (str.equalsIgnoreCase(s)) { + return true; + } + } + return false; + } } } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsHook.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsHook.java index d694635a17..8a3cb888b2 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsHook.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/MetricsHook.java @@ -11,6 +11,11 @@ package net.sourceforge.pmd.lang.java.oom; */ public class MetricsHook { + private MetricsHook() { + + } + + public static void reset() { Metrics.reset(); } diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/SigMaskTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/SigMaskTest.java index e4552f7fa0..101e1e7d7f 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/SigMaskTest.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/SigMaskTest.java @@ -46,6 +46,9 @@ public class SigMaskTest extends ParserTst { + "}"; private static final String TEST_OPERATIONS = "abstract class Bzaz{ " + + "int x;" + + "int y;" + + "int z;" // constructors + "public Bzaz() {}" + "private Bzaz(int x){}" @@ -65,10 +68,10 @@ public class SigMaskTest extends ParserTst { + "protected void foo(int x){} " + "private void rand(){}" // abstract - + "protected abstract int getXAbs();" + + "protected abstract int getZ();" + "abstract int abs2();" + "public static abstract String abstr();" - + "abstract int setXAbs();" + + "abstract void setZ(int x);" + "}"; /** diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/testdata/GetterDetection.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/testdata/GetterDetection.java index 53b3fdefcc..ea5a826091 100644 --- a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/testdata/GetterDetection.java +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/oom/testdata/GetterDetection.java @@ -22,7 +22,7 @@ public class GetterDetection { } - public boolean isBooleanTrue() { + public boolean isBool() { return bool; }