001: ////////////////////////////////////////////////////////////////////////////////
002: // checkstyle: Checks Java source code for adherence to a set of rules.
003: // Copyright (C) 2001-2007 Oliver Burn
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: ////////////////////////////////////////////////////////////////////////////////
019:
020: package com.puppycrawl.tools.checkstyle.checks.imports;
021:
022: import com.puppycrawl.tools.checkstyle.api.Check;
023: import com.puppycrawl.tools.checkstyle.api.DetailAST;
024: import com.puppycrawl.tools.checkstyle.api.FullIdent;
025: import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026:
027: /**
028: * <p>
029: * Checks for imports from a set of illegal packages.
030: * By default, the check rejects all <code>sun.*</code> packages
031: * since programs that contain direct calls to the <code>sun.*</code> packages
032: * are <a href="http://java.sun.com/products/jdk/faq/faq-sun-packages.html">
033: * not 100% Pure Java</a>.
034: * </p>
035: * <p>
036: * To reject other packages, set property illegalPkgs to a comma-separated
037: * list of the illegal packages.
038: * </p>
039: * <p>
040: * An example of how to configure the check is:
041: * </p>
042: * <pre>
043: * <module name="IllegalImport"/>
044: * </pre>
045: * <p>
046: * An example of how to configure the check so that it rejects packages
047: * <code>java.io.*</code> and <code>java.sql.*</code> is
048: * </p>
049: * <pre>
050: * <module name="IllegalImport">
051: * <property name="illegalPkgs" value="java.io, java.sql"/>
052: * </module>
053: *
054: * Compatible with Java 1.5 source.
055: *
056: * </pre>
057: * @author Oliver Burn
058: * @author Lars Kühne
059: * @version 1.0
060: */
061: public class IllegalImportCheck extends Check {
062: /** list of illegal packages */
063: private String[] mIllegalPkgs;
064:
065: /**
066: * Creates a new <code>IllegalImportCheck</code> instance.
067: */
068: public IllegalImportCheck() {
069: setIllegalPkgs(new String[] { "sun" });
070: }
071:
072: /**
073: * Set the list of illegal packages.
074: * @param aFrom array of illegal packages
075: */
076: public void setIllegalPkgs(String[] aFrom) {
077: mIllegalPkgs = aFrom;
078: }
079:
080: /** {@inheritDoc} */
081: public int[] getDefaultTokens() {
082: return new int[] { TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT };
083: }
084:
085: /** {@inheritDoc} */
086: public void visitToken(DetailAST aAST) {
087: final FullIdent imp;
088: if (aAST.getType() == TokenTypes.IMPORT) {
089: imp = FullIdent.createFullIdentBelow(aAST);
090: } else {
091: imp = FullIdent.createFullIdent((DetailAST) aAST
092: .getFirstChild().getNextSibling());
093: }
094: if (isIllegalImport(imp.getText())) {
095: log(aAST.getLineNo(), aAST.getColumnNo(), "import.illegal",
096: imp.getText());
097: }
098: }
099:
100: /**
101: * Checks if an import is from a package that must not be used.
102: * @param aImportText the argument of the import keyword
103: * @return if <code>aImportText</code> contains an illegal package prefix
104: */
105: private boolean isIllegalImport(String aImportText) {
106: for (int i = 0; i < mIllegalPkgs.length; i++) {
107: if (aImportText.startsWith(mIllegalPkgs[i] + ".")) {
108: return true;
109: }
110: }
111: return false;
112: }
113: }
|