001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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 com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.FullIdent; 026import com.puppycrawl.tools.checkstyle.api.TokenTypes; 027import com.puppycrawl.tools.checkstyle.utils.CommonUtil; 028 029/** 030 * <div> 031 * Checks that there are no static import statements. 032 * </div> 033 * 034 * <p> 035 * Rationale: Importing static members can lead to naming conflicts 036 * between class' members. It may lead to poor code readability since it 037 * may no longer be clear what class a member resides in (without looking 038 * at the import statement). 039 * </p> 040 * 041 * <p> 042 * Notes: 043 * If you exclude a starred import on a class this automatically excludes 044 * each member individually. 045 * </p> 046 * 047 * <p> 048 * For example: Excluding {@code java.lang.Math.*}. will allow the import 049 * of each static member in the Math class individually like 050 * {@code java.lang.Math.PI, java.lang.Math.cos, ...}. 051 * </p> 052 * 053 * @since 5.0 054 */ 055@StatelessCheck 056public class AvoidStaticImportCheck 057 extends AbstractCheck { 058 059 /** 060 * A key is pointing to the warning message text in "messages.properties" 061 * file. 062 */ 063 public static final String MSG_KEY = "import.avoidStatic"; 064 065 /** 066 * Control whether to allow for certain classes via a star notation to be 067 * excluded such as {@code java.lang.Math.*} or specific static members 068 * to be excluded like {@code java.lang.System.out} for a variable or 069 * {@code java.lang.Math.random} for a method. See notes section for details. 070 */ 071 private String[] excludes = CommonUtil.EMPTY_STRING_ARRAY; 072 073 @Override 074 public int[] getDefaultTokens() { 075 return getRequiredTokens(); 076 } 077 078 @Override 079 public int[] getAcceptableTokens() { 080 return getRequiredTokens(); 081 } 082 083 @Override 084 public int[] getRequiredTokens() { 085 return new int[] {TokenTypes.STATIC_IMPORT}; 086 } 087 088 /** 089 * Setter to control whether to allow for certain classes via a star notation 090 * to be excluded such as {@code java.lang.Math.*} or specific static members 091 * to be excluded like {@code java.lang.System.out} for a variable or 092 * {@code java.lang.Math.random} for a method. See notes section for details. 093 * 094 * @param excludes fully-qualified class names/specific 095 * static members where static imports are ok 096 * @since 5.0 097 */ 098 public void setExcludes(String... excludes) { 099 this.excludes = excludes.clone(); 100 } 101 102 @Override 103 public void visitToken(final DetailAST ast) { 104 final DetailAST startingDot = 105 ast.getFirstChild().getNextSibling(); 106 final FullIdent name = FullIdent.createFullIdent(startingDot); 107 108 final String nameText = name.getText(); 109 if (!isExempt(nameText)) { 110 log(startingDot, MSG_KEY, nameText); 111 } 112 } 113 114 /** 115 * Checks if a class or static member is exempt from known excludes. 116 * 117 * @param classOrStaticMember 118 * the class or static member 119 * @return true if except false if not 120 */ 121 private boolean isExempt(String classOrStaticMember) { 122 boolean exempt = false; 123 124 for (String exclude : excludes) { 125 if (classOrStaticMember.equals(exclude) 126 || isStarImportOfPackage(classOrStaticMember, exclude)) { 127 exempt = true; 128 break; 129 } 130 } 131 return exempt; 132 } 133 134 /** 135 * Returns true if classOrStaticMember is a starred name of package, 136 * not just member name. 137 * 138 * @param classOrStaticMember - full name of member 139 * @param exclude - current exclusion 140 * @return true if member in exclusion list 141 */ 142 private static boolean isStarImportOfPackage(String classOrStaticMember, String exclude) { 143 boolean result = false; 144 if (exclude.endsWith(".*")) { 145 // this section allows explicit imports 146 // to be exempt when configured using 147 // a starred import 148 final String excludeMinusDotStar = 149 exclude.substring(0, exclude.length() - 2); 150 if (classOrStaticMember.startsWith(excludeMinusDotStar) 151 && !classOrStaticMember.equals(excludeMinusDotStar)) { 152 final String member = classOrStaticMember.substring( 153 excludeMinusDotStar.length() + 1); 154 // if it contains a dot then it is not a member but a package 155 if (member.indexOf('.') == -1) { 156 result = true; 157 } 158 } 159 } 160 return result; 161 } 162 163}