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