From 1910a6844c643504108ddb37b39fbd59082d90cc Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 18 Apr 2015 10:04:31 +0200 Subject: [PATCH] #1332 False Positive: UnusedPrivateMethod --- .../pmd/lang/java/symboltable/ClassScope.java | 17 ++++++---- .../unusedcode/xml/UnusedPrivateMethod.xml | 34 +++++++++++++++++++ src/site/markdown/overview/changelog.md | 1 + 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java index 8492b78f2a..146ce503aa 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/symboltable/ClassScope.java @@ -4,8 +4,10 @@ package net.sourceforge.pmd.lang.java.symboltable; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; @@ -310,19 +312,20 @@ public class ClassScope extends AbstractJavaScope { if (typeImage == null) { return null; } - for (String qualified : this.getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().keySet()) { + + Set qualifiedNames = new LinkedHashSet(); + qualifiedNames.addAll(this.getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().keySet()); + qualifiedNames.addAll(this.getEnclosingScope(SourceFileScope.class).getExplicitImports()); + + int nameLength = typeImage.length(); + + for (String qualified : qualifiedNames) { int fullLength = qualified.length(); - int nameLength = typeImage.length(); if (qualified.endsWith(typeImage) && (fullLength == nameLength || qualified.substring(0, fullLength - nameLength).endsWith("."))) { return qualified; } } - for (String qualified : this.getEnclosingScope(SourceFileScope.class).getExplicitImports()) { - if (qualified.endsWith(typeImage)) { - return qualified; - } - } return typeImage; } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml index d90a123518..b33300cfb4 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/unusedcode/xml/UnusedPrivateMethod.xml @@ -1412,4 +1412,38 @@ public class UnusedPrivateMethod { } ]]> + + + #1332 False Positive: UnusedPrivateMethod + 0 + + diff --git a/src/site/markdown/overview/changelog.md b/src/site/markdown/overview/changelog.md index b5656e5fb0..80197a283f 100644 --- a/src/site/markdown/overview/changelog.md +++ b/src/site/markdown/overview/changelog.md @@ -14,6 +14,7 @@ **Bugfixes:** +* [#1332](https://sourceforge.net/p/pmd/bugs/1332/): False Positive: UnusedPrivateMethod * [#1333](https://sourceforge.net/p/pmd/bugs/1333/): Error while processing Java file with Lambda expressions * [#1338](https://sourceforge.net/p/pmd/bugs/1338/): The pmd-java8 POM bears the wrong parent module version