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