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