
org.walkmod.checkstyle.treewalkers.AvoidStarImport Maven / Gradle / Ivy
package org.walkmod.checkstyle.treewalkers;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.walkmod.javalang.ASTManager;
import org.walkmod.javalang.ParseException;
import org.walkmod.javalang.ast.CompilationUnit;
import org.walkmod.javalang.ast.ImportDeclaration;
import org.walkmod.javalang.ast.SymbolData;
import org.walkmod.javalang.ast.SymbolDataAware;
import org.walkmod.javalang.ast.SymbolReference;
import org.walkmod.javalang.ast.expr.NameExpr;
import org.walkmod.javalang.compiler.symbols.RequiresSemanticAnalysis;
import org.walkmod.javalang.visitors.VoidVisitorAdapter;
@RequiresSemanticAnalysis
public class AvoidStarImport extends VoidVisitorAdapter {
@Override
public void visit(ImportDeclaration node, A ctx) {
if (node.isAsterisk() && !node.isStatic()) {
//support for static imports require to take into account not only types, but also methods and properties
Map> classes = expandWildCardImports(node);
removeWildcardImports(node, classes);
}
}
private Map> expandWildCardImports(ImportDeclaration node) {
List refs = node.getUsages();
Map> classes = new HashMap>();
if (refs != null) {
for (SymbolReference sr : refs) {
if (sr instanceof SymbolDataAware>) {
SymbolDataAware> aux = (SymbolDataAware>) sr;
SymbolData sd = aux.getSymbolData();
if (sd != null && ! sd.getClazz().isMemberClass()) {
List refsAux = null;
String symbolClassName = sd.getName();
if (classes.containsKey(symbolClassName)) {
refsAux = classes.get(symbolClassName);
} else {
refsAux = new LinkedList();
classes.put(symbolClassName, refsAux);
}
refsAux.add(sr);
}
}
}
}
return classes;
}
private void removeWildcardImports(ImportDeclaration node, Map> classes) {
CompilationUnit cu = (CompilationUnit) node.getParentNode();
List imports = new LinkedList(cu.getImports());
Iterator it = imports.iterator();
int i = -1;
boolean errored = false;
boolean found = false;
while (it.hasNext() && !found) {
i++;
ImportDeclaration current = it.next();
found = (current == node);
}
if (found) {
it.remove();
if (!classes.isEmpty()) {
for (String clazz : classes.keySet()) {
ImportDeclaration id;
try {
id = new ImportDeclaration((NameExpr) ASTManager.parse(NameExpr.class, clazz), node.isStatic(),
false);
List refsAux = classes.get(clazz);
for(SymbolReference sr: refsAux){
sr.setSymbolDefinition(id);
}
id.setUsages(refsAux);
imports.add(i, id);
} catch (ParseException e) {
errored = true;
}
}
}
}
if (!errored) {
cu.setImports(imports);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy