org.walkmod.checkstyle.treewalkers.UnusedImports Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of walkmod-checkstyle-plugin Show documentation
Show all versions of walkmod-checkstyle-plugin Show documentation
Walkmod plugin to automatically fix the reported problems by Checkstyle
package org.walkmod.checkstyle.treewalkers;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.walkmod.checkstyle.visitors.AbstractCheckStyleRule;
import org.walkmod.javalang.ast.CompilationUnit;
import org.walkmod.javalang.ast.ImportDeclaration;
import org.walkmod.javalang.ast.SymbolReference;
import org.walkmod.javalang.compiler.symbols.RequiresSemanticAnalysis;
@RequiresSemanticAnalysis
public class UnusedImports extends AbstractCheckStyleRule {
@Override
public void visit(ImportDeclaration node, A ctx) {
List usages = node.getUsages();
if (usages == null || usages.isEmpty()) {
CompilationUnit cu = (CompilationUnit) node.getParentNode();
List imports = new LinkedList(cu.getImports());
Iterator it = imports.iterator();
boolean found = false;
while (it.hasNext() && !found) {
ImportDeclaration current = it.next();
found = (current == node);
}
if (found) {
it.remove();
cu.setImports(imports);
}
}
}
}