#914 False +ve from UnusedImports with wildcard static imports
This commit is contained in:
@ -3,17 +3,45 @@
|
||||
*/
|
||||
package net.sourceforge.pmd.lang.rule;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
|
||||
public class ImportWrapper {
|
||||
private Node node;
|
||||
private String name;
|
||||
private String fullname;
|
||||
private boolean isStaticDemand;
|
||||
private Set<String> allDemands = new HashSet<String>();
|
||||
|
||||
public ImportWrapper(String fullname, String name) {
|
||||
this(fullname, name, null);
|
||||
}
|
||||
|
||||
public ImportWrapper(String fullname, String name, Node node) {
|
||||
this(fullname, name, node, false);
|
||||
}
|
||||
|
||||
public ImportWrapper(String fullname, String name, Node node, Class<?> type, boolean isStaticDemand) {
|
||||
this(fullname, name, node, isStaticDemand);
|
||||
if (type != null) {
|
||||
for (Method m : type.getMethods()) {
|
||||
allDemands.add(m.getName());
|
||||
}
|
||||
for (Field f : type.getFields()) {
|
||||
allDemands.add(f.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ImportWrapper(String fullname, String name, Node node, boolean isStaticDemand) {
|
||||
this.fullname = fullname;
|
||||
this.name = name;
|
||||
this.node = node;
|
||||
this.isStaticDemand = isStaticDemand;
|
||||
}
|
||||
|
||||
public boolean equals(Object other) {
|
||||
@ -33,6 +61,18 @@ public class ImportWrapper {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean matches(ImportWrapper i) {
|
||||
if (isStaticDemand) {
|
||||
if (allDemands.contains(i.fullname)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (name == null && i.getName() == null) {
|
||||
return i.getFullName().equals(fullname);
|
||||
}
|
||||
return i.getName().equals(name);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
if(name == null){
|
||||
return fullname.hashCode();
|
||||
@ -51,5 +91,10 @@ public class ImportWrapper {
|
||||
public Node getNode() {
|
||||
return node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Import[name=" + name + ",fullname=" + fullname + ",static*=" + isStaticDemand + "]";
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user