1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2025 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.imports;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.FullIdent;
26 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
27 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28
29 /**
30 * <div>
31 * Checks that there are no static import statements.
32 * </div>
33 *
34 * <p>
35 * Rationale: Importing static members can lead to naming conflicts
36 * between class' members. It may lead to poor code readability since it
37 * may no longer be clear what class a member resides in (without looking
38 * at the import statement).
39 * </p>
40 *
41 * <p>
42 * Notes:
43 * If you exclude a starred import on a class this automatically excludes
44 * each member individually.
45 * </p>
46 *
47 * <p>
48 * For example: Excluding {@code java.lang.Math.*}. will allow the import
49 * of each static member in the Math class individually like
50 * {@code java.lang.Math.PI, java.lang.Math.cos, ...}.
51 * </p>
52 *
53 * @since 5.0
54 */
55 @StatelessCheck
56 public class AvoidStaticImportCheck
57 extends AbstractCheck {
58
59 /**
60 * A key is pointing to the warning message text in "messages.properties"
61 * file.
62 */
63 public static final String MSG_KEY = "import.avoidStatic";
64
65 /**
66 * Control whether to allow for certain classes via a star notation to be
67 * excluded such as {@code java.lang.Math.*} or specific static members
68 * to be excluded like {@code java.lang.System.out} for a variable or
69 * {@code java.lang.Math.random} for a method. See notes section for details.
70 */
71 private String[] excludes = CommonUtil.EMPTY_STRING_ARRAY;
72
73 @Override
74 public int[] getDefaultTokens() {
75 return getRequiredTokens();
76 }
77
78 @Override
79 public int[] getAcceptableTokens() {
80 return getRequiredTokens();
81 }
82
83 @Override
84 public int[] getRequiredTokens() {
85 return new int[] {TokenTypes.STATIC_IMPORT};
86 }
87
88 /**
89 * Setter to control whether to allow for certain classes via a star notation
90 * to be excluded such as {@code java.lang.Math.*} or specific static members
91 * to be excluded like {@code java.lang.System.out} for a variable or
92 * {@code java.lang.Math.random} for a method. See notes section for details.
93 *
94 * @param excludes fully-qualified class names/specific
95 * static members where static imports are ok
96 * @since 5.0
97 */
98 public void setExcludes(String... excludes) {
99 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 }