KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 package com.puppycrawl.tools.checkstyle.checks.imports;
21
22 import com.puppycrawl.tools.checkstyle.api.Check;
23 import com.puppycrawl.tools.checkstyle.api.DetailAST;
24 import com.puppycrawl.tools.checkstyle.api.FullIdent;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
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 imports that are redundant. An import statement is
34  * considered redundant if:
35  * </p>
36  *<ul>
37  * <li>It is a duplicate of another import. This is, when a class is imported
38  * more than once.</li>
39  * <li>The class non-statically imported is from the <code>java.lang</code>
40  * package. For example importing <code>java.lang.String</code>.</li>
41  * <li>The class non-statically imported is from the same package as the
42  * current package.</li>
43  *</ul>
44  * <p>
45  * An example of how to configure the check is:
46  * </p>
47  * <pre>
48  * &lt;module name="RedundantImport"/&gt;
49  * </pre>
50  *
51  * Compatible with Java 1.5 source.
52  *
53  * @author Oliver Burn
54  * @version 1.0
55  */

56 public class RedundantImportCheck
57     extends Check
58 {
59     /** name of package in file */
60     private String JavaDoc mPkgName;
61     /** set of the imports */
62     private final Set JavaDoc mImports = new HashSet JavaDoc();
63     /** set of static imports */
64     private final Set JavaDoc mStaticImports = new HashSet JavaDoc();
65
66     /** {@inheritDoc} */
67     public void beginTree(DetailAST aRootAST)
68     {
69         mPkgName = null;
70         mImports.clear();
71         mStaticImports.clear();
72     }
73
74     /** {@inheritDoc} */
75     public int[] getDefaultTokens()
76     {
77         return new int[]
78         {TokenTypes.IMPORT,
79          TokenTypes.STATIC_IMPORT,
80          TokenTypes.PACKAGE_DEF, };
81     }
82
83     /** {@inheritDoc} */
84     public void visitToken(DetailAST aAST)
85     {
86         if (aAST.getType() == TokenTypes.PACKAGE_DEF) {
87             mPkgName = FullIdent.createFullIdent(
88                     aAST.getLastChild().getPreviousSibling()).getText();
89         }
90         else if (aAST.getType() == TokenTypes.IMPORT) {
91             final FullIdent imp = FullIdent.createFullIdentBelow(aAST);
92             if (fromPackage(imp.getText(), "java.lang")) {
93                 log(aAST.getLineNo(), aAST.getColumnNo(), "import.lang",
94                     imp.getText());
95             }
96             else if (fromPackage(imp.getText(), mPkgName)) {
97                 log(aAST.getLineNo(), aAST.getColumnNo(), "import.same",
98                     imp.getText());
99             }
100             // Check for a duplicate import
101
final Iterator JavaDoc it = mImports.iterator();
102             while (it.hasNext()) {
103                 final FullIdent full = (FullIdent) it.next();
104                 if (imp.getText().equals(full.getText())) {
105                     log(aAST.getLineNo(),
106                         aAST.getColumnNo(),
107                         "import.duplicate",
108                         new Integer JavaDoc(full.getLineNo()),
109                         imp.getText());
110                 }
111             }
112
113             mImports.add(imp);
114         }
115         else {
116             // Check for a duplicate static import
117
final FullIdent imp =
118                 FullIdent.createFullIdent(
119                     aAST.getLastChild().getPreviousSibling());
120             final Iterator JavaDoc it = mStaticImports.iterator();
121             while (it.hasNext()) {
122                 final FullIdent full = (FullIdent) it.next();
123                 if (imp.getText().equals(full.getText())) {
124                     log(aAST.getLineNo(),
125                         aAST.getColumnNo(),
126                         "import.duplicate",
127                         new Integer JavaDoc(full.getLineNo()),
128                         imp.getText());
129                 }
130             }
131
132             mStaticImports.add(imp);
133         }
134     }
135
136     /**
137      * Determines if an import statement is for types from a specified package.
138      * @param aImport the import name
139      * @param aPkg the package name
140      * @return whether from the package
141      */

142     private static boolean fromPackage(String JavaDoc aImport, String JavaDoc aPkg)
143     {
144         boolean retVal = false;
145         if (aPkg == null) {
146             // If not package, then check for no package in the import.
147
retVal = (aImport.indexOf('.') == -1);
148         }
149         else {
150             final int index = aImport.lastIndexOf('.');
151             if (index != -1) {
152                 final String JavaDoc front = aImport.substring(0, index);
153                 retVal = front.equals(aPkg);
154             }
155         }
156         return retVal;
157     }
158 }
159
Popular Tags