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