001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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 java.util.HashSet;
023import java.util.Set;
024
025import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
026import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
027import com.puppycrawl.tools.checkstyle.api.DetailAST;
028import com.puppycrawl.tools.checkstyle.api.FullIdent;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030
031/**
032 * <p>
033 * Checks for redundant import statements. An import statement is
034 * considered redundant if:
035 * </p>
036 *<ul>
037 *  <li>It is a duplicate of another import. This is, when a class is imported
038 *  more than once.</li>
039 *  <li>The class non-statically imported is from the {@code java.lang}
040 *  package, e.g. importing {@code java.lang.String}.</li>
041 *  <li>The class non-statically imported is from the same package as the
042 *  current package.</li>
043 *</ul>
044 * <p>
045 * To configure the check:
046 * </p>
047 * <pre>
048 * &lt;module name="RedundantImport"/&gt;
049 * </pre>
050 *
051 * @since 3.0
052 */
053@FileStatefulCheck
054public class RedundantImportCheck
055    extends AbstractCheck {
056
057    /**
058     * A key is pointing to the warning message text in "messages.properties"
059     * file.
060     */
061    public static final String MSG_LANG = "import.lang";
062
063    /**
064     * A key is pointing to the warning message text in "messages.properties"
065     * file.
066     */
067    public static final String MSG_SAME = "import.same";
068
069    /**
070     * A key is pointing to the warning message text in "messages.properties"
071     * file.
072     */
073    public static final String MSG_DUPLICATE = "import.duplicate";
074
075    /** Set of the imports. */
076    private final Set<FullIdent> imports = new HashSet<>();
077    /** Set of static imports. */
078    private final Set<FullIdent> staticImports = new HashSet<>();
079
080    /** Name of package in file. */
081    private String pkgName;
082
083    @Override
084    public void beginTree(DetailAST aRootAST) {
085        pkgName = null;
086        imports.clear();
087        staticImports.clear();
088    }
089
090    @Override
091    public int[] getDefaultTokens() {
092        return getRequiredTokens();
093    }
094
095    @Override
096    public int[] getAcceptableTokens() {
097        return getRequiredTokens();
098    }
099
100    @Override
101    public int[] getRequiredTokens() {
102        return new int[] {
103            TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT, TokenTypes.PACKAGE_DEF,
104        };
105    }
106
107    @Override
108    public void visitToken(DetailAST ast) {
109        if (ast.getType() == TokenTypes.PACKAGE_DEF) {
110            pkgName = FullIdent.createFullIdent(
111                    ast.getLastChild().getPreviousSibling()).getText();
112        }
113        else if (ast.getType() == TokenTypes.IMPORT) {
114            final FullIdent imp = FullIdent.createFullIdentBelow(ast);
115            if (isFromPackage(imp.getText(), "java.lang")) {
116                log(ast, MSG_LANG, imp.getText());
117            }
118            // imports from unnamed package are not allowed,
119            // so we are checking SAME rule only for named packages
120            else if (pkgName != null && isFromPackage(imp.getText(), pkgName)) {
121                log(ast, MSG_SAME, imp.getText());
122            }
123            // Check for a duplicate import
124            imports.stream().filter(full -> imp.getText().equals(full.getText()))
125                .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText()));
126
127            imports.add(imp);
128        }
129        else {
130            // Check for a duplicate static import
131            final FullIdent imp =
132                FullIdent.createFullIdent(
133                    ast.getLastChild().getPreviousSibling());
134            staticImports.stream().filter(full -> imp.getText().equals(full.getText()))
135                .forEach(full -> log(ast, MSG_DUPLICATE, full.getLineNo(), imp.getText()));
136
137            staticImports.add(imp);
138        }
139    }
140
141    /**
142     * Determines if an import statement is for types from a specified package.
143     * @param importName the import name
144     * @param pkg the package name
145     * @return whether from the package
146     */
147    private static boolean isFromPackage(String importName, String pkg) {
148        // imports from unnamed package are not allowed:
149        // https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5
150        // So '.' must be present in member name and we are not checking for it
151        final int index = importName.lastIndexOf('.');
152        final String front = importName.substring(0, index);
153        return front.equals(pkg);
154    }
155
156}