001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 the original author or authors.
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.*} packages
031 * since programs that contain direct calls to the {@code sun.*} packages
032 * are <a href="http://www.oracle.com/technetwork/java/faq-sun-packages-142232.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.*} and {@code java.sql.*} 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 */
060public class IllegalImportCheck
061    extends Check {
062
063    /**
064     * A key is pointing to the warning message text in "messages.properties"
065     * file.
066     */
067    public static final String MSG_KEY = "import.illegal";
068
069    /** List of illegal packages. */
070    private String[] illegalPkgs;
071
072    /**
073     * Creates a new {@code IllegalImportCheck} instance.
074     */
075    public IllegalImportCheck() {
076        setIllegalPkgs("sun");
077    }
078
079    /**
080     * Set the list of illegal packages.
081     * @param from array of illegal packages
082     */
083    public final void setIllegalPkgs(String... from) {
084        illegalPkgs = from.clone();
085    }
086
087    @Override
088    public int[] getDefaultTokens() {
089        return getAcceptableTokens();
090    }
091
092    @Override
093    public int[] getAcceptableTokens() {
094        return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
095    }
096
097    @Override
098    public int[] getRequiredTokens() {
099        return getAcceptableTokens();
100    }
101
102    @Override
103    public void visitToken(DetailAST ast) {
104        final FullIdent imp;
105        if (ast.getType() == TokenTypes.IMPORT) {
106            imp = FullIdent.createFullIdentBelow(ast);
107        }
108        else {
109            imp = FullIdent.createFullIdent(
110                ast.getFirstChild().getNextSibling());
111        }
112        if (isIllegalImport(imp.getText())) {
113            log(ast.getLineNo(),
114                ast.getColumnNo(),
115                MSG_KEY,
116                imp.getText());
117        }
118    }
119
120    /**
121     * Checks if an import is from a package that must not be used.
122     * @param importText the argument of the import keyword
123     * @return if {@code importText} contains an illegal package prefix
124     */
125    private boolean isIllegalImport(String importText) {
126        for (String element : illegalPkgs) {
127            if (importText.startsWith(element + ".")) {
128                return true;
129            }
130        }
131        return false;
132    }
133}