use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.
the class UnusedImportsRule method visit.
@Override
public Object visit(ASTImportDeclaration node, Object data) {
if (node.isImportOnDemand()) {
ASTName importedType = (ASTName) node.jjtGetChild(0);
imports.add(new ImportWrapper(importedType.getImage(), null, node, node.getType(), node.isStatic()));
} else {
if (!node.isImportOnDemand()) {
ASTName importedType = (ASTName) node.jjtGetChild(0);
String className;
if (isQualifiedName(importedType)) {
int lastDot = importedType.getImage().lastIndexOf('.') + 1;
className = importedType.getImage().substring(lastDot);
} else {
className = importedType.getImage();
}
imports.add(new ImportWrapper(importedType.getImage(), className, node));
}
}
return data;
}
use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.
the class UnusedImportsRule method check.
protected void check(Node node) {
if (imports.isEmpty()) {
return;
}
ImportWrapper candidate = getImportWrapper(node);
Iterator<ImportWrapper> it = imports.iterator();
while (it.hasNext()) {
ImportWrapper i = it.next();
if (i.matches(candidate)) {
it.remove();
return;
}
}
if (TypeNode.class.isAssignableFrom(node.getClass()) && ((TypeNode) node).getType() != null) {
Class<?> c = ((TypeNode) node).getType();
if (c.getPackage() != null) {
candidate = new ImportWrapper(c.getPackage().getName(), null);
if (imports.contains(candidate)) {
imports.remove(candidate);
}
}
}
}
use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.
the class UnusedImportsRule method visit.
@Override
public Object visit(ASTCompilationUnit node, Object data) {
imports.clear();
super.visit(node, data);
visitComments(node);
/*
* special handling for Bug 2606609 : False "UnusedImports" positive in
* package-info.java package annotations are processed before the import
* clauses so they need to be examined again later on.
*/
if (node.jjtGetNumChildren() > 0 && node.jjtGetChild(0) instanceof ASTPackageDeclaration) {
visit((ASTPackageDeclaration) node.jjtGetChild(0), data);
}
for (ImportWrapper wrapper : imports) {
addViolation(data, wrapper.getNode(), wrapper.getFullName());
}
return data;
}
use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.
the class DuplicateImportsRule method visit.
public Object visit(ASTCompilationUnit node, Object data) {
singleTypeImports = new HashSet<>();
importOnDemandImports = new HashSet<>();
super.visit(node, data);
// import java.io.File;
for (ImportWrapper thisImportOnDemand : importOnDemandImports) {
for (ImportWrapper thisSingleTypeImport : singleTypeImports) {
// java.io.File
String singleTypeFullName = thisSingleTypeImport.getName();
int lastDot = singleTypeFullName.lastIndexOf('.');
// java.io
String singleTypePkg = singleTypeFullName.substring(0, lastDot);
// File
String singleTypeName = singleTypeFullName.substring(lastDot + 1);
if (thisImportOnDemand.getName().equals(singleTypePkg) && !isDisambiguationImport(node, singleTypePkg, singleTypeName)) {
addViolation(data, thisSingleTypeImport.getNode(), singleTypeFullName);
}
}
}
singleTypeImports.clear();
importOnDemandImports.clear();
return data;
}
use of net.sourceforge.pmd.lang.rule.ImportWrapper in project pmd by pmd.
the class DuplicateJspImportsRule method visit.
@Override
public Object visit(ASTJspDirectiveAttribute node, Object data) {
if (!"import".equals(node.getName())) {
return super.visit(node, data);
}
String values = node.getValue();
StringTokenizer st = new StringTokenizer(values, ",");
int count = st.countTokens();
for (int ix = 0; ix < count; ix++) {
String token = st.nextToken();
ImportWrapper wrapper = new ImportWrapper(token, token, node);
if (imports.contains(wrapper)) {
addViolation(data, node, node.getImage());
} else {
imports.add(wrapper);
}
}
return super.visit(node, data);
}
Aggregations