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

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

There is a newer version: 1.0.12
Show newest version
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);
         }
         
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy