KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
28  * <p>
29  * Checks for imports from a set of illegal packages.
30  * By default, the check rejects all <code>sun.*</code> packages
31  * since programs that contain direct calls to the <code>sun.*</code> packages
32  * are <a HREF="http://java.sun.com/products/jdk/faq/faq-sun-packages.html">
33  * not 100% Pure Java</a>.
34  * </p>
35  * <p>
36  * To reject other packages, set property illegalPkgs to a comma-separated
37  * list of the illegal packages.
38  * </p>
39  * <p>
40  * An example of how to configure the check is:
41  * </p>
42  * <pre>
43  * &lt;module name="IllegalImport"/&gt;
44  * </pre>
45  * <p>
46  * An example of how to configure the check so that it rejects packages
47  * <code>java.io.*</code> and <code>java.sql.*</code> is
48  * </p>
49  * <pre>
50  * &lt;module name="IllegalImport"&gt;
51  * &lt;property name="illegalPkgs" value="java.io, java.sql"/&gt;
52  * &lt;/module&gt;
53  *
54  * Compatible with Java 1.5 source.
55  *
56  * </pre>
57  * @author Oliver Burn
58  * @author Lars Kühne
59  * @version 1.0
60  */

61 public class IllegalImportCheck
62     extends Check
63 {
64     /** list of illegal packages */
65     private String JavaDoc[] mIllegalPkgs;
66
67     /**
68      * Creates a new <code>IllegalImportCheck</code> instance.
69      */

70     public IllegalImportCheck()
71     {
72         setIllegalPkgs(new String JavaDoc[] {"sun"});
73     }
74
75     /**
76      * Set the list of illegal packages.
77      * @param aFrom array of illegal packages
78      */

79     public void setIllegalPkgs(String JavaDoc[] aFrom)
80     {
81         mIllegalPkgs = aFrom;
82     }
83
84     /** {@inheritDoc} */
85     public int[] getDefaultTokens()
86     {
87         return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
88     }
89
90     /** {@inheritDoc} */
91     public void visitToken(DetailAST aAST)
92     {
93         final FullIdent imp;
94         if (aAST.getType() == TokenTypes.IMPORT) {
95             imp = FullIdent.createFullIdentBelow(aAST);
96         }
97         else {
98             imp = FullIdent.createFullIdent(
99                 (DetailAST) aAST.getFirstChild().getNextSibling());
100         }
101         if (isIllegalImport(imp.getText())) {
102             log(aAST.getLineNo(),
103                 aAST.getColumnNo(),
104                 "import.illegal",
105                 imp.getText());
106         }
107     }
108
109     /**
110      * Checks if an import is from a package that must not be used.
111      * @param aImportText the argument of the import keyword
112      * @return if <code>aImportText</code> contains an illegal package prefix
113      */

114     private boolean isIllegalImport(String JavaDoc aImportText)
115     {
116         for (int i = 0; i < mIllegalPkgs.length; i++) {
117             if (aImportText.startsWith(mIllegalPkgs[i] + ".")) {
118                 return true;
119             }
120         }
121         return false;
122     }
123 }
124
Popular Tags