1 package com.puppycrawl.tools.checkstyle.checks.imports; 20 21 import com.puppycrawl.tools.checkstyle.api.DetailAST; 22 import com.puppycrawl.tools.checkstyle.api.FullIdent; 23 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 24 import com.puppycrawl.tools.checkstyle.api.Utils; 25 import com.puppycrawl.tools.checkstyle.checks.DeclarationCollector; 26 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 47 public class UnusedImportsCheck extends DeclarationCollector 48 { 49 50 private boolean mCollect; 51 52 53 private final Set mImports = new HashSet (); 54 55 56 private final Set mReferenced = new HashSet (); 57 58 59 public UnusedImportsCheck() 60 { 61 } 62 63 64 public void beginTree(DetailAST aRootAST) 65 { 66 super.beginTree(aRootAST); 67 mCollect = false; 68 mImports.clear(); 69 mReferenced.clear(); 70 } 71 72 73 public void finishTree(DetailAST aRootAST) 74 { 75 final Iterator it = mImports.iterator(); 77 while (it.hasNext()) { 78 final FullIdent imp = (FullIdent) it.next(); 79 80 if (!mReferenced.contains(Utils.baseClassname(imp.getText()))) { 81 log(imp.getLineNo(), 82 imp.getColumnNo(), 83 "import.unused", imp.getText()); 84 } 85 } 86 } 87 88 89 public int[] getDefaultTokens() 90 { 91 return new int[] { 92 TokenTypes.PACKAGE_DEF, 93 TokenTypes.ANNOTATION_DEF, 94 TokenTypes.CLASS_DEF, 95 TokenTypes.CTOR_DEF, 96 TokenTypes.ENUM_DEF, 97 TokenTypes.IDENT, 98 TokenTypes.IMPORT, 99 TokenTypes.INTERFACE_DEF, 100 TokenTypes.METHOD_DEF, 101 TokenTypes.PARAMETER_DEF, 102 TokenTypes.SLIST, 103 TokenTypes.STATIC_IMPORT, 104 TokenTypes.VARIABLE_DEF, 105 }; 106 } 107 108 109 public int[] getRequiredTokens() 110 { 111 return getDefaultTokens(); 112 } 113 114 115 public void visitToken(DetailAST aAST) 116 { 117 super.visitToken(aAST); 118 if (aAST.getType() == TokenTypes.IDENT) { 119 if (mCollect) { 120 processIdent(aAST); 121 } 122 } 123 else if (aAST.getType() == TokenTypes.IMPORT) { 124 processImport(aAST); 125 } 126 else if (aAST.getType() == TokenTypes.STATIC_IMPORT) { 127 processStaticImport(aAST); 128 } 129 else if ((aAST.getType() == TokenTypes.CLASS_DEF) 130 || (aAST.getType() == TokenTypes.INTERFACE_DEF) 131 || (aAST.getType() == TokenTypes.ENUM_DEF) 132 || (aAST.getType() == TokenTypes.ANNOTATION_DEF) 133 || (aAST.getType() == TokenTypes.PACKAGE_DEF)) 134 { 135 mCollect = true; 136 } 137 } 138 139 143 private void processIdent(DetailAST aAST) 144 { 145 final DetailAST parent = aAST.getParent(); 146 final int parentType = parent.getType(); 147 if (((parentType != TokenTypes.DOT) 148 && (parentType != TokenTypes.METHOD_DEF)) 149 || ((parentType == TokenTypes.DOT) 150 && (aAST.getNextSibling() != null))) 151 { 152 if (!isDeclared(aAST.getText())) { 153 mReferenced.add(aAST.getText()); 154 } 155 } 156 } 157 158 162 private void processImport(DetailAST aAST) 163 { 164 final FullIdent name = FullIdent.createFullIdentBelow(aAST); 165 if ((name != null) && !name.getText().endsWith(".*")) { 166 mImports.add(name); 167 } 168 } 169 170 174 private void processStaticImport(DetailAST aAST) 175 { 176 final FullIdent name = 177 FullIdent.createFullIdent( 178 (DetailAST) aAST.getFirstChild().getNextSibling()); 179 if ((name != null) && !name.getText().endsWith(".*")) { 180 mImports.add(name); 181 } 182 } 183 } 184 | Popular Tags |