001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 * To configure the check: 060 * </p> 061 * <pre> 062 * <module name="AvoidStaticImport"/> 063 * </pre> 064 * <p> 065 * To configure the check so that the {@code java.lang.System.out} member and all 066 * members from {@code java.lang.Math} are allowed: 067 * </p> 068 * <pre> 069 * <module name="AvoidStaticImport"> 070 * <property name="excludes" value="java.lang.System.out,java.lang.Math.*"/> 071 * </module> 072 * </pre> 073 * <p> 074 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker} 075 * </p> 076 * <p> 077 * Violation Message Keys: 078 * </p> 079 * <ul> 080 * <li> 081 * {@code import.avoidStatic} 082 * </li> 083 * </ul> 084 * 085 * @since 5.0 086 */ 087@StatelessCheck 088public class AvoidStaticImportCheck 089 extends AbstractCheck { 090 091 /** 092 * A key is pointing to the warning message text in "messages.properties" 093 * file. 094 */ 095 public static final String MSG_KEY = "import.avoidStatic"; 096 097 /** 098 * Control whether to allow for certain classes via a star notation to be 099 * excluded such as {@code java.lang.Math.*} or specific static members 100 * to be excluded like {@code java.lang.System.out} for a variable or 101 * {@code java.lang.Math.random} for a method. See notes section for details. 102 */ 103 private String[] excludes = CommonUtil.EMPTY_STRING_ARRAY; 104 105 @Override 106 public int[] getDefaultTokens() { 107 return getRequiredTokens(); 108 } 109 110 @Override 111 public int[] getAcceptableTokens() { 112 return getRequiredTokens(); 113 } 114 115 @Override 116 public int[] getRequiredTokens() { 117 return new int[] {TokenTypes.STATIC_IMPORT}; 118 } 119 120 /** 121 * Setter to control whether to allow for certain classes via a star notation 122 * to be excluded such as {@code java.lang.Math.*} or specific static members 123 * to be excluded like {@code java.lang.System.out} for a variable or 124 * {@code java.lang.Math.random} for a method. See notes section for details. 125 * 126 * @param excludes a list of fully-qualified class names/specific 127 * static members where static imports are ok 128 */ 129 public void setExcludes(String... excludes) { 130 this.excludes = excludes.clone(); 131 } 132 133 @Override 134 public void visitToken(final DetailAST ast) { 135 final DetailAST startingDot = 136 ast.getFirstChild().getNextSibling(); 137 final FullIdent name = FullIdent.createFullIdent(startingDot); 138 139 if (!isExempt(name.getText())) { 140 log(startingDot, MSG_KEY, name.getText()); 141 } 142 } 143 144 /** 145 * Checks if a class or static member is exempt from known excludes. 146 * 147 * @param classOrStaticMember 148 * the class or static member 149 * @return true if except false if not 150 */ 151 private boolean isExempt(String classOrStaticMember) { 152 boolean exempt = false; 153 154 for (String exclude : excludes) { 155 if (classOrStaticMember.equals(exclude) 156 || isStarImportOfPackage(classOrStaticMember, exclude)) { 157 exempt = true; 158 break; 159 } 160 } 161 return exempt; 162 } 163 164 /** 165 * Returns true if classOrStaticMember is a starred name of package, 166 * not just member name. 167 * 168 * @param classOrStaticMember - full name of member 169 * @param exclude - current exclusion 170 * @return true if member in exclusion list 171 */ 172 private static boolean isStarImportOfPackage(String classOrStaticMember, String exclude) { 173 boolean result = false; 174 if (exclude.endsWith(".*")) { 175 // this section allows explicit imports 176 // to be exempt when configured using 177 // a starred import 178 final String excludeMinusDotStar = 179 exclude.substring(0, exclude.length() - 2); 180 if (classOrStaticMember.startsWith(excludeMinusDotStar) 181 && !classOrStaticMember.equals(excludeMinusDotStar)) { 182 final String member = classOrStaticMember.substring( 183 excludeMinusDotStar.length() + 1); 184 // if it contains a dot then it is not a member but a package 185 if (member.indexOf('.') == -1) { 186 result = true; 187 } 188 } 189 } 190 return result; 191 } 192 193}