001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2024 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.StatelessCheck;
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 that there are no import statements that use the {@code *} notation.
034 * </p>
035 * <p>
036 * Rationale: Importing all classes from a package or static
037 * members from a class leads to tight coupling between packages
038 * or classes and might lead to problems when a new version of a
039 * library introduces name clashes.
040 * </p>
041 * <p>
042 * Note that property {@code excludes} is not recursive, subpackages of excluded
043 * packages are not automatically excluded.
044 * </p>
045 * <ul>
046 * <li>
047 * Property {@code allowClassImports} - Control whether to allow starred class
048 * imports like {@code import java.util.*;}.
049 * Type is {@code boolean}.
050 * Default value is {@code false}.
051 * </li>
052 * <li>
053 * Property {@code allowStaticMemberImports} - Control whether to allow starred
054 * static member imports like {@code import static org.junit.Assert.*;}.
055 * Type is {@code boolean}.
056 * Default value is {@code false}.
057 * </li>
058 * <li>
059 * Property {@code excludes} - Specify packages where starred class imports are
060 * allowed and classes where starred static member imports are allowed.
061 * Type is {@code java.lang.String[]}.
062 * Default value is {@code ""}.
063 * </li>
064 * </ul>
065 * <p>
066 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
067 * </p>
068 * <p>
069 * Violation Message Keys:
070 * </p>
071 * <ul>
072 * <li>
073 * {@code import.avoidStar}
074 * </li>
075 * </ul>
076 *
077 * @since 3.0
078 */
079@StatelessCheck
080public class AvoidStarImportCheck
081    extends AbstractCheck {
082
083    /**
084     * A key is pointing to the warning message text in "messages.properties"
085     * file.
086     */
087    public static final String MSG_KEY = "import.avoidStar";
088
089    /** Suffix for the star import. */
090    private static final String STAR_IMPORT_SUFFIX = ".*";
091
092    /**
093     * Specify packages where starred class imports are
094     * allowed and classes where starred static member imports are allowed.
095     */
096    private final Set<String> excludes = new HashSet<>();
097
098    /**
099     * Control whether to allow starred class imports like
100     * {@code import java.util.*;}.
101     */
102    private boolean allowClassImports;
103
104    /**
105     * Control whether to allow starred static member imports like
106     * {@code import static org.junit.Assert.*;}.
107     */
108    private boolean allowStaticMemberImports;
109
110    @Override
111    public int[] getDefaultTokens() {
112        return getRequiredTokens();
113    }
114
115    @Override
116    public int[] getAcceptableTokens() {
117        return getRequiredTokens();
118    }
119
120    @Override
121    public int[] getRequiredTokens() {
122        // original implementation checks both IMPORT and STATIC_IMPORT tokens to avoid ".*" imports
123        // however user can allow using "import" or "import static"
124        // by configuring allowClassImports and allowStaticMemberImports
125        // To avoid potential confusion when user specifies conflicting options on configuration
126        // (see example below) we are adding both tokens to Required list
127        //   <module name="AvoidStarImport">
128        //      <property name="tokens" value="IMPORT"/>
129        //      <property name="allowStaticMemberImports" value="false"/>
130        //   </module>
131        return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
132    }
133
134    /**
135     * Setter to specify packages where starred class imports are
136     * allowed and classes where starred static member imports are allowed.
137     *
138     * @param excludesParam package names/fully-qualifies class names
139     *     where star imports are ok
140     * @since 3.2
141     */
142    public void setExcludes(String... excludesParam) {
143        for (final String exclude : excludesParam) {
144            if (exclude.endsWith(STAR_IMPORT_SUFFIX)) {
145                excludes.add(exclude);
146            }
147            else {
148                excludes.add(exclude + STAR_IMPORT_SUFFIX);
149            }
150        }
151    }
152
153    /**
154     * Setter to control whether to allow starred class imports like
155     * {@code import java.util.*;}.
156     *
157     * @param allow true to allow false to disallow
158     * @since 5.3
159     */
160    public void setAllowClassImports(boolean allow) {
161        allowClassImports = allow;
162    }
163
164    /**
165     * Setter to control whether to allow starred static member imports like
166     * {@code import static org.junit.Assert.*;}.
167     *
168     * @param allow true to allow false to disallow
169     * @since 5.3
170     */
171    public void setAllowStaticMemberImports(boolean allow) {
172        allowStaticMemberImports = allow;
173    }
174
175    @Override
176    public void visitToken(final DetailAST ast) {
177        if (ast.getType() == TokenTypes.IMPORT) {
178            if (!allowClassImports) {
179                final DetailAST startingDot = ast.getFirstChild();
180                logsStarredImportViolation(startingDot);
181            }
182        }
183        else if (!allowStaticMemberImports) {
184            // must navigate past the static keyword
185            final DetailAST startingDot = ast.getFirstChild().getNextSibling();
186            logsStarredImportViolation(startingDot);
187        }
188    }
189
190    /**
191     * Gets the full import identifier.  If the import is a starred import and
192     * it's not excluded then a violation is logged.
193     *
194     * @param startingDot the starting dot for the import statement
195     */
196    private void logsStarredImportViolation(DetailAST startingDot) {
197        final FullIdent name = FullIdent.createFullIdent(startingDot);
198        final String importText = name.getText();
199        if (importText.endsWith(STAR_IMPORT_SUFFIX) && !excludes.contains(importText)) {
200            log(startingDot, MSG_KEY, importText);
201        }
202    }
203
204}