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

org.walkmod.checkstyle.treewalkers.RedundantImport Maven / Gradle / Ivy

There is a newer version: 1.0.12
Show newest version
package org.walkmod.checkstyle.treewalkers;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.PackageDeclaration;
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 RedundantImport extends AbstractCheckStyleRule {

   private Map mapping;

   private String pakage = null;

   private Map> asteriskImports = new HashMap>();

   @Override
   public void visit(CompilationUnit cu, A ctx) {
      List imports = cu.getImports();
      if (imports != null) {
         PackageDeclaration pkg = cu.getPackage();
         if (pkg != null) {
            pakage = pkg.getName().toString();
         }
         mapping = new HashMap();

         Iterator it = imports.iterator();
         while (it.hasNext()) {
            ImportDeclaration id = it.next();
            if (id.isAsterisk()) {
               asteriskImports.put(id.getName().toString(), new HashSet());
            }

         }

         it = imports.iterator();
         while (it.hasNext()) {
            ImportDeclaration id = it.next();
            String name = id.getName().toString();
            if (!id.isAsterisk()) {
               mapping.put(name, id);
               int index = name.lastIndexOf(".");
               if (index != -1) {
                  String pkgName = name.substring(0, index);
                  if (asteriskImports.containsKey(pkgName)) {
                     Set relatedImports = asteriskImports.get(pkgName);
                     relatedImports.add(name);
                  }
               }
            }
         }
         ImportsCleaner ic = new ImportsCleaner();
         it = imports.iterator();
         while (it.hasNext()) {
            it.next().accept(ic, ctx);
         }
         cu.setImports(ic.getImports());

      }
   }

   private class ImportsCleaner extends VoidVisitorAdapter {

      private List correctImports = new LinkedList();

      public List getImports() {
         return correctImports;
      }

      @Override
      public void visit(ImportDeclaration node, A ctx) {

         String importedName = node.getName().toString();

         ImportDeclaration aux = mapping.get(importedName);
         if (aux != null) {
            if (pakage == null || !importedName.startsWith(pakage)) {
               if (aux == node) {
                  if (!importedName.startsWith("java.lang")) {
                     correctImports.add(node);
                  }
               } else {
                  if (node.isStatic()) {
                     correctImports.add(node);
                  }
               }
            }
         } else {
            //isAsterisk
            if (pakage == null || !importedName.startsWith(pakage)) {
               List usages = node.getUsages();
               Set relatedImports = asteriskImports.get(importedName);
               Map> importsToAdd = new HashMap>();
               if (usages != null) {
                  Iterator it = usages.iterator();
                  while (it.hasNext()) {
                     SymbolReference current = it.next();
                     if (current instanceof SymbolDataAware) {
                        SymbolDataAware sda = (SymbolDataAware) current;
                        String typeName = sda.getSymbolData().getName();
                        if (!relatedImports.contains(typeName)) {
                           List references = new LinkedList();
                           references.add(current);
                           importsToAdd.put(typeName, references);
                        } else {
                           importsToAdd.get(typeName).add(current);
                        }

                     }
                  }
               } else if (node.isNewNode()) {
                  correctImports.add(node); //we don't know if it is needed by other new nodes.
               }
               if (!importsToAdd.isEmpty()) {
                  Iterator it = importsToAdd.keySet().iterator();
                  while (it.hasNext()) {
                     String next = it.next();
                     NameExpr name;
                     try {
                        name = (NameExpr) ASTManager.parse(NameExpr.class, next);
                        ImportDeclaration id = new ImportDeclaration(name, false, false);
                        List references = importsToAdd.get(next);
                        for (SymbolReference ref : references) {
                           ref.setSymbolDefinition(id);
                        }
                        id.setUsages(references);
                        correctImports.add(id);
                     } catch (ParseException e) {
                        throw new RuntimeException(e);
                     }

                  }
               }
            }
         }
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy