diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java index b7ad4e8b6b..fd248a50f9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codestyle/UnnecessaryFullyQualifiedNameRule.java @@ -107,12 +107,12 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { } private void checkImports(TypeNode node, Object data) { - String name = node.getImage(); + final String name = node.getImage(); // variable names shadow everything else // If the first segment is a variable, then all // the following are field accesses and it's not an FQCN - if (isVariable(node)) { + if (isVariable(node.getScope(), name)) { return; } @@ -184,7 +184,7 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { if (matches.isEmpty()) { if (isJavaLangImplicit(node)) { addViolation(data, node, new Object[] { node.getImage(), "java.lang.*", "implicit "}); - } else if (isSamePackage(node)) { + } else if (isSamePackage(name)) { addViolation(data, node, new Object[] { node.getImage(), currentPackage + ".*", "same package "}); } } else { @@ -223,19 +223,13 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { return result; } - private boolean isVariable(TypeNode node) { - String name = node.getImage(); + private boolean isVariable(Scope scope, String name) { String firstSegment = name.substring(0, name.indexOf('.')); - return isVariableInScope(node.getScope(), firstSegment); - } - - private boolean isVariableInScope(Scope scope, String name) { - while (scope != null) { for (Entry> entry : scope.getDeclarations(VariableNameDeclaration.class).entrySet()) { - if (entry.getKey().getName().equals(name)) { + if (entry.getKey().getName().equals(firstSegment)) { return true; } } @@ -246,9 +240,17 @@ public class UnnecessaryFullyQualifiedNameRule extends AbstractJavaRule { return false; } - private boolean isSamePackage(TypeNode node) { - String name = node.getImage(); - return name.substring(0, name.lastIndexOf('.')).equals(currentPackage); + private boolean isSamePackage(String name) { + int i = name.lastIndexOf('.'); + while (i > 0) { + name = name.substring(0, i); + if (name.equals(currentPackage)) { + return true; + } + i = name.lastIndexOf('.'); + } + + return false; } private boolean isJavaLangImplicit(TypeNode node) { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml index cf87255b74..25a35a57dc 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UnnecessaryFullyQualifiedName.xml @@ -488,7 +488,7 @@ public class TestArrayType { } ]]> - + #1199 false negative for same package FQCN 1 @@ -504,7 +504,7 @@ public class SamePackage { - #1951 false positive when package name is shadowed by variable + #1951 false positive when package name is obscured by variable 0 - False positive when package name is shadowed by variable (2) + False positive when package name is obscured by variable (2) 0 - False positive when type name is shadowed by variable + False positive when type name is obscured by variable 0