[java] UnusedImports false positive for static import

Fixes #925
This commit is contained in:
Andreas Dangel
2018-02-21 20:37:49 +01:00
parent bff93c53fb
commit cc08bd47cf
4 changed files with 44 additions and 0 deletions

View File

@@ -37,6 +37,7 @@ This is a minor release.
* [#920](https://github.com/pmd/pmd/pull/920): \[java] Update valid identifiers in grammar
* java-bestpractices
* [#784](https://github.com/pmd/pmd/issues/784): \[java] ForLoopCanBeForeach false-positive
* [#925](https://github.com/pmd/pmd/issues/925): \[java] UnusedImports false positive for static import
* java-design
* [#855](https://github.com/pmd/pmd/issues/855): \[java] ImmutableField false-positive with lambdas
* java-documentation

View File

@@ -6,6 +6,7 @@ package net.sourceforge.pmd.lang.rule;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;
@@ -35,6 +36,13 @@ public class ImportWrapper {
for (Field f : type.getFields()) {
allDemands.add(f.getName());
}
// also consider static fields, that are not public
int requiredMod = Modifier.STATIC;
for (Field f : type.getDeclaredFields()) {
if ((f.getModifiers() & requiredMod) == requiredMod) {
allDemands.add(f.getName());
}
}
}
}

View File

@@ -0,0 +1,18 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
import java.util.Arrays;
import java.util.List;
public class ClassWithConstants {
private ClassWithConstants() {
// Utility class
}
/*package*/ static final List<String> LIST1 = Arrays.asList("A");
/*package*/ static final List<String> LIST2 = Arrays.asList("B");
}

View File

@@ -395,4 +395,21 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>#925 [java] UnusedImports false positive for static import</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.ClassWithConstants.*;
public class ClassWithImport {
public static void main(String[] args) {
System.out.println("List 1: " + LIST1);
System.out.println("List 2: " + LIST2);
}
}
]]></code>
</test-code>
</test-data>