All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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.checkstyle.visitors.AbstractCheckStyleRule;
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;

@RequiresSemanticAnalysis
public class AvoidStarImport extends AbstractCheckStyleRule {

   @Override
   public void visit(ImportDeclaration node, A ctx) {
      if (node.isAsterisk()) {
         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) {
                     List refsAux = null;
                     if (classes.containsKey(sd.getName())) {
                        refsAux = classes.get(sd.getName());
                     } else {
                        refsAux = new LinkedList();
                        classes.put(sd.getName(), refsAux);
                     }
                     refsAux.add(sr);

                  }
               }
            }
         }
         CompilationUnit cu = (CompilationUnit) node.getParentNode();
         List imports = new LinkedList(cu.getImports());
         Iterator it = imports.iterator();
         int i = -1;
         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) {
                     throw new RuntimeException(e);
                  }
               }
            }
         }
         cu.setImports(imports);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy