KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > imports > UnusedImportsCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 Oliver Burn
4
//
5
// This library is free software; you can redistribute it and/or
6
// modify it under the terms of the GNU Lesser General Public
7
// License as published by the Free Software Foundation; either
8
// version 2.1 of the License, or (at your option) any later version.
9
//
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
// Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public
16
// License along with this library; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
////////////////////////////////////////////////////////////////////////////////
19
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 JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * <p>
33  * Checks for unused import statements.
34  * </p>
35  * <p>
36  * An example of how to configure the check is:
37  * </p>
38  * <pre>
39  * &lt;module name="UnusedImports"/&gt;
40  * </pre>
41  *
42  * Compatible with Java 1.5 source.
43  *
44  * @author Oliver Burn
45  * @version 1.1
46  */

47 public class UnusedImportsCheck extends DeclarationCollector
48 {
49     /** flag to indicate when time to start collecting references */
50     private boolean mCollect;
51
52     /** set of the imports */
53     private final Set JavaDoc mImports = new HashSet JavaDoc();
54
55     /** set of references - possibly to imports or other things */
56     private final Set JavaDoc mReferenced = new HashSet JavaDoc();
57
58     /** Default constructor. */
59     public UnusedImportsCheck()
60     {
61     }
62
63     /** {@inheritDoc} */
64     public void beginTree(DetailAST aRootAST)
65     {
66         super.beginTree(aRootAST);
67         mCollect = false;
68         mImports.clear();
69         mReferenced.clear();
70     }
71
72     /** {@inheritDoc} */
73     public void finishTree(DetailAST aRootAST)
74     {
75         // loop over all the imports to see if referenced.
76
final Iterator JavaDoc 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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
109     public int[] getRequiredTokens()
110     {
111         return getDefaultTokens();
112     }
113
114     /** {@inheritDoc} */
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     /**
140      * Collects references made by IDENT.
141      * @param aAST the IDENT node to process
142      */

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     /**
159      * Collects the details of imports.
160      * @param aAST node containing the import details
161      */

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     /**
171      * Collects the details of static imports.
172      * @param aAST node containing the static import details
173      */

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