[java] UnusedImports: False positive if wildcard is used and only static methods
Added workaround for wrong typeresolution (method result vs. class of method) Refs #2016
This commit is contained in:
@ -143,6 +143,23 @@ public class UnusedImportsRule extends AbstractJavaRule {
|
||||
|
||||
@Override
|
||||
public Object visit(ASTName node, Object data) {
|
||||
if (isMethodCall(node) && isQualifiedName(node)) {
|
||||
String name = node.getImage().substring(0, node.getImage().lastIndexOf('.'));
|
||||
// try to resolve with on demand imports...
|
||||
ClassTypeResolver classTypeResolver = node.getFirstParentOfType(ASTCompilationUnit.class).getClassTypeResolver();
|
||||
Iterator<ImportWrapper> it = imports.iterator();
|
||||
while (it.hasNext()) {
|
||||
ImportWrapper i = it.next();
|
||||
if (i.getName() == null) {
|
||||
String fullName = i.getFullName() + "." + name;
|
||||
if (classTypeResolver.loadClass(fullName) != null) {
|
||||
// found a match
|
||||
it.remove();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
check(node);
|
||||
return data;
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 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.*;
|
||||
|
||||
/**
|
||||
* Note: In order for this test case to work, the class "Issue2016" must also be compiled and available
|
||||
* on the auxclasspath.
|
||||
*/
|
||||
public class Issue2016 {
|
||||
public void testFunction() {
|
||||
Objects.toString(null);
|
||||
}
|
||||
}
|
@ -531,4 +531,24 @@ public class VendingV2PaymentRequest {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#2016 [java] UnusedImports: False positive if wildcard is used and only static methods</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
package net.sourceforge.pmd.lang.java.rule.bestpractices.unusedimports;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Note: In order for this test case to work, the class "Issue2016" must also be compiled and available
|
||||
* on the auxclasspath.
|
||||
*/
|
||||
public class Issue2016 {
|
||||
public void testFunction() {
|
||||
Objects.toString(null);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
||||
|
Reference in New Issue
Block a user