#1332 False Positive: UnusedPrivateMethod

This commit is contained in:
Andreas Dangel
2015-04-18 10:04:31 +02:00
parent 342e0c4789
commit 1910a6844c
3 changed files with 45 additions and 7 deletions

View File

@ -4,8 +4,10 @@
package net.sourceforge.pmd.lang.java.symboltable; package net.sourceforge.pmd.lang.java.symboltable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression; import net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression;
@ -310,19 +312,20 @@ public class ClassScope extends AbstractJavaScope {
if (typeImage == null) { if (typeImage == null) {
return null; return null;
} }
for (String qualified : this.getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().keySet()) {
int fullLength = qualified.length(); Set<String> qualifiedNames = new LinkedHashSet<String>();
qualifiedNames.addAll(this.getEnclosingScope(SourceFileScope.class).getQualifiedTypeNames().keySet());
qualifiedNames.addAll(this.getEnclosingScope(SourceFileScope.class).getExplicitImports());
int nameLength = typeImage.length(); int nameLength = typeImage.length();
for (String qualified : qualifiedNames) {
int fullLength = qualified.length();
if (qualified.endsWith(typeImage) if (qualified.endsWith(typeImage)
&& (fullLength == nameLength || qualified.substring(0, fullLength - nameLength).endsWith("."))) { && (fullLength == nameLength || qualified.substring(0, fullLength - nameLength).endsWith("."))) {
return qualified; return qualified;
} }
} }
for (String qualified : this.getEnclosingScope(SourceFileScope.class).getExplicitImports()) {
if (qualified.endsWith(typeImage)) {
return qualified;
}
}
return typeImage; return typeImage;
} }

View File

@ -1409,6 +1409,40 @@ public class UnusedPrivateMethod {
private void deleteAllAssetsWithExceptions(int galleryId, boolean purge, Integer... exceptionList) throws MediaServiceException private void deleteAllAssetsWithExceptions(int galleryId, boolean purge, Integer... exceptionList) throws MediaServiceException
{ {
} }
}
]]></code>
</test-code>
<test-code>
<description>#1332 False Positive: UnusedPrivateMethod</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package com.acme;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class FooTest {
@Test
public void testBar() {
assertThat(createBar("", ""), containsString(""));
}
@Test
public void testFoo() {
assertThat(createFoo(""), containsString(""));
}
private String createFoo(String a) {
return a;
}
private String createBar(String b, String c) {
return b + c;
}
} }
]]></code> ]]></code>
</test-code> </test-code>

View File

@ -14,6 +14,7 @@
**Bugfixes:** **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 * [#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 * [#1338](https://sourceforge.net/p/pmd/bugs/1338/): The pmd-java8 POM bears the wrong parent module version