001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  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
020package com.puppycrawl.tools.checkstyle.checks.imports;
021
022import com.puppycrawl.tools.checkstyle.api.Check;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.FullIdent;
025import 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 * &lt;module name="IllegalImport"/&gt;
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 * &lt;module name="IllegalImport"&gt;
051 *    &lt;property name="illegalPkgs" value="java.io, java.sql"/&gt;
052 * &lt;/module&gt;
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 */
061public class IllegalImportCheck
062    extends Check
063{
064    /** list of illegal packages */
065    private String[] illegalPkgs;
066
067    /**
068     * Creates a new <code>IllegalImportCheck</code> instance.
069     */
070    public IllegalImportCheck()
071    {
072        setIllegalPkgs(new String[] {"sun"});
073    }
074
075    /**
076     * Set the list of illegal packages.
077     * @param from array of illegal packages
078     */
079    public void setIllegalPkgs(String[] from)
080    {
081        illegalPkgs = from.clone();
082    }
083
084    @Override
085    public int[] getDefaultTokens()
086    {
087        return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
088    }
089
090    @Override
091    public void visitToken(DetailAST ast)
092    {
093        final FullIdent imp;
094        if (ast.getType() == TokenTypes.IMPORT) {
095            imp = FullIdent.createFullIdentBelow(ast);
096        }
097        else {
098            imp = FullIdent.createFullIdent(
099                ast.getFirstChild().getNextSibling());
100        }
101        if (isIllegalImport(imp.getText())) {
102            log(ast.getLineNo(),
103                ast.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 importText the argument of the import keyword
112     * @return if <code>importText</code> contains an illegal package prefix
113     */
114    private boolean isIllegalImport(String importText)
115    {
116        for (String element : illegalPkgs) {
117            if (importText.startsWith(element + ".")) {
118                return true;
119            }
120        }
121        return false;
122    }
123}