1 /////////////////////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules. 3 // Copyright (C) 2001-2025 the original author or authors. 4 // 5 // This library is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 2.1 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 /////////////////////////////////////////////////////////////////////////////////////////////// 19 20 package com.puppycrawl.tools.checkstyle.checks.imports; 21 22 import java.util.HashSet; 23 import java.util.Set; 24 25 import com.puppycrawl.tools.checkstyle.StatelessCheck; 26 import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 27 import com.puppycrawl.tools.checkstyle.api.DetailAST; 28 import com.puppycrawl.tools.checkstyle.api.FullIdent; 29 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 30 31 /** 32 * <div> 33 * Checks that there are no import statements that use the {@code *} notation. 34 * </div> 35 * 36 * <p> 37 * Rationale: Importing all classes from a package or static 38 * members from a class leads to tight coupling between packages 39 * or classes and might lead to problems when a new version of a 40 * library introduces name clashes. 41 * </p> 42 * 43 * <p> 44 * Notes: 45 * Note that property {@code excludes} is not recursive, subpackages of excluded 46 * packages are not automatically excluded. 47 * </p> 48 * <ul> 49 * <li> 50 * Property {@code allowClassImports} - Control whether to allow starred class 51 * imports like {@code import java.util.*;}. 52 * Type is {@code boolean}. 53 * Default value is {@code false}. 54 * </li> 55 * <li> 56 * Property {@code allowStaticMemberImports} - Control whether to allow starred 57 * static member imports like {@code import static org.junit.Assert.*;}. 58 * Type is {@code boolean}. 59 * Default value is {@code false}. 60 * </li> 61 * <li> 62 * Property {@code excludes} - Specify packages where starred class imports are 63 * allowed and classes where starred static member imports are allowed. 64 * Type is {@code java.lang.String[]}. 65 * Default value is {@code ""}. 66 * </li> 67 * </ul> 68 * 69 * <p> 70 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 71 * </p> 72 * 73 * <p> 74 * Violation Message Keys: 75 * </p> 76 * <ul> 77 * <li> 78 * {@code import.avoidStar} 79 * </li> 80 * </ul> 81 * 82 * @since 3.0 83 */ 84 @StatelessCheck 85 public class AvoidStarImportCheck 86 extends AbstractCheck { 87 88 /** 89 * A key is pointing to the warning message text in "messages.properties" 90 * file. 91 */ 92 public static final String MSG_KEY = "import.avoidStar"; 93 94 /** Suffix for the star import. */ 95 private static final String STAR_IMPORT_SUFFIX = ".*"; 96 97 /** 98 * Specify packages where starred class imports are 99 * allowed and classes where starred static member imports are allowed. 100 */ 101 private final Set<String> excludes = new HashSet<>(); 102 103 /** 104 * Control whether to allow starred class imports like 105 * {@code import java.util.*;}. 106 */ 107 private boolean allowClassImports; 108 109 /** 110 * Control whether to allow starred static member imports like 111 * {@code import static org.junit.Assert.*;}. 112 */ 113 private boolean allowStaticMemberImports; 114 115 @Override 116 public int[] getDefaultTokens() { 117 return getRequiredTokens(); 118 } 119 120 @Override 121 public int[] getAcceptableTokens() { 122 return getRequiredTokens(); 123 } 124 125 @Override 126 public int[] getRequiredTokens() { 127 // original implementation checks both IMPORT and STATIC_IMPORT tokens to avoid ".*" imports 128 // however user can allow using "import" or "import static" 129 // by configuring allowClassImports and allowStaticMemberImports 130 // To avoid potential confusion when user specifies conflicting options on configuration 131 // (see example below) we are adding both tokens to Required list 132 // <module name="AvoidStarImport"> 133 // <property name="tokens" value="IMPORT"/> 134 // <property name="allowStaticMemberImports" value="false"/> 135 // </module> 136 return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT}; 137 } 138 139 /** 140 * Setter to specify packages where starred class imports are 141 * allowed and classes where starred static member imports are allowed. 142 * 143 * @param excludesParam package names/fully-qualifies class names 144 * where star imports are ok 145 * @since 3.2 146 */ 147 public void setExcludes(String... excludesParam) { 148 for (final String exclude : excludesParam) { 149 if (exclude.endsWith(STAR_IMPORT_SUFFIX)) { 150 excludes.add(exclude); 151 } 152 else { 153 excludes.add(exclude + STAR_IMPORT_SUFFIX); 154 } 155 } 156 } 157 158 /** 159 * Setter to control whether to allow starred class imports like 160 * {@code import java.util.*;}. 161 * 162 * @param allow true to allow false to disallow 163 * @since 5.3 164 */ 165 public void setAllowClassImports(boolean allow) { 166 allowClassImports = allow; 167 } 168 169 /** 170 * Setter to control whether to allow starred static member imports like 171 * {@code import static org.junit.Assert.*;}. 172 * 173 * @param allow true to allow false to disallow 174 * @since 5.3 175 */ 176 public void setAllowStaticMemberImports(boolean allow) { 177 allowStaticMemberImports = allow; 178 } 179 180 @Override 181 public void visitToken(final DetailAST ast) { 182 if (ast.getType() == TokenTypes.IMPORT) { 183 if (!allowClassImports) { 184 final DetailAST startingDot = ast.getFirstChild(); 185 logsStarredImportViolation(startingDot); 186 } 187 } 188 else if (!allowStaticMemberImports) { 189 // must navigate past the static keyword 190 final DetailAST startingDot = ast.getFirstChild().getNextSibling(); 191 logsStarredImportViolation(startingDot); 192 } 193 } 194 195 /** 196 * Gets the full import identifier. If the import is a starred import and 197 * it's not excluded then a violation is logged. 198 * 199 * @param startingDot the starting dot for the import statement 200 */ 201 private void logsStarredImportViolation(DetailAST startingDot) { 202 final FullIdent name = FullIdent.createFullIdent(startingDot); 203 final String importText = name.getText(); 204 if (importText.endsWith(STAR_IMPORT_SUFFIX) && !excludes.contains(importText)) { 205 log(startingDot, MSG_KEY, importText); 206 } 207 } 208 209 }