[java] UnusedImports false positive for static import with package-private method usage

Fixes #1209
This commit is contained in:
Andreas Dangel
2018-10-24 19:46:58 +02:00
parent d3880ed349
commit 666dc36343
5 changed files with 75 additions and 0 deletions

View File

@ -30,6 +30,7 @@ as comments are recognized as such and ignored.
* [#1330](https://github.com/pmd/pmd/issues/1330): \[java] PMD crashes with java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/xml/ws/Service
* java-bestpractices
* [#1202](https://github.com/pmd/pmd/issues/1202): \[java] GuardLogStatement: "There is log block not surrounded by if" doesn't sound right
* [#1209](https://github.com/pmd/pmd/issues/1209): \[java] UnusedImports false positive for static import with package-private method usage
* [#1365](https://github.com/pmd/pmd/issues/1365): \[java] JUnitTestsShouldIncludeAssert false positive
* java-codestyle
* [#1199](https://github.com/pmd/pmd/issues/1199): \[java] UnnecessaryFullyQualifiedName doesn't flag same package FQCNs

View File

@ -42,6 +42,12 @@ public class ImportWrapper {
allDemands.add(f.getName());
}
}
// and methods, too
for (Method m : type.getDeclaredMethods()) {
if (Modifier.isStatic(m.getModifiers())) {
allDemands.add(m.getName());
}
}
}
}

View File

@ -0,0 +1,22 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
final class PackagePrivateUtils {
private PackagePrivateUtils() {
}
static int f1(int x) {
return x + 1;
}
static int f2(int x) {
return x + 1;
}
static int f3(int x) {
return x + 1;
}
}

View File

@ -0,0 +1,22 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
public class PublicUtils {
private PublicUtils() {
}
public static int g1(int x) {
return x + 1;
}
public static int g2(int x) {
return x + 1;
}
public static int g3(int x) {
return x + 1;
}
}

View File

@ -409,6 +409,30 @@ public class ClassWithImport {
System.out.println("List 1: " + LIST1);
System.out.println("List 2: " + LIST2);
}
}
]]></code>
</test-code>
<test-code>
<description>#1209 [java] UnusedImports false positive for static import with package-private method usage</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.PackagePrivateUtils.*;
import static net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports.PublicUtils.*;
public class Imports {
int importtest() {
int i = 0;
i = f1(i);
i = g1(i);
i = f2(i);
i = g2(i);
i = f3(i);
i = g3(i);
return i;
}
}
]]></code>
</test-code>