1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.checks.imports;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Pattern;
25
26 import com.puppycrawl.tools.checkstyle.StatelessCheck;
27 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
28 import com.puppycrawl.tools.checkstyle.api.DetailAST;
29 import com.puppycrawl.tools.checkstyle.api.FullIdent;
30 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @StatelessCheck
65 public class IllegalImportCheck
66 extends AbstractCheck {
67
68
69
70
71
72 public static final String MSG_KEY = "import.illegal";
73
74
75 private final List<Pattern> illegalPkgsRegexps = new ArrayList<>();
76
77
78 private final List<Pattern> illegalClassesRegexps = new ArrayList<>();
79
80
81 private String[] illegalPkgs;
82
83
84 private String[] illegalClasses;
85
86
87
88
89
90 private boolean regexp;
91
92
93
94
95 public IllegalImportCheck() {
96 setIllegalPkgs("sun");
97 }
98
99
100
101
102
103
104 public final void setIllegalPkgs(String... from) {
105 illegalPkgs = from.clone();
106 illegalPkgsRegexps.clear();
107 for (String illegalPkg : illegalPkgs) {
108 illegalPkgsRegexps.add(CommonUtil.createPattern("^" + illegalPkg + "\\..*"));
109 }
110 }
111
112
113
114
115
116 public void setIllegalClasses(String... from) {
117 illegalClasses = from.clone();
118 for (String illegalClass : illegalClasses) {
119 illegalClassesRegexps.add(CommonUtil.createPattern(illegalClass));
120 }
121 }
122
123
124
125
126
127
128 public void setRegexp(boolean regexp) {
129 this.regexp = regexp;
130 }
131
132 @Override
133 public int[] getDefaultTokens() {
134 return getRequiredTokens();
135 }
136
137 @Override
138 public int[] getAcceptableTokens() {
139 return getRequiredTokens();
140 }
141
142 @Override
143 public int[] getRequiredTokens() {
144 return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
145 }
146
147 @Override
148 public void visitToken(DetailAST ast) {
149 final FullIdent imp;
150 if (ast.getType() == TokenTypes.IMPORT) {
151 imp = FullIdent.createFullIdentBelow(ast);
152 }
153 else {
154 imp = FullIdent.createFullIdent(
155 ast.getFirstChild().getNextSibling());
156 }
157 if (isIllegalImport(imp.getText())) {
158 log(ast,
159 MSG_KEY,
160 imp.getText());
161 }
162 }
163
164
165
166
167
168
169
170
171 private boolean isIllegalImportByRegularExpressions(String importText) {
172 boolean result = false;
173 for (Pattern pattern : illegalPkgsRegexps) {
174 if (pattern.matcher(importText).matches()) {
175 result = true;
176 break;
177 }
178 }
179 if (!result) {
180 for (Pattern pattern : illegalClassesRegexps) {
181 if (pattern.matcher(importText).matches()) {
182 result = true;
183 break;
184 }
185 }
186 }
187 return result;
188 }
189
190
191
192
193
194
195 private boolean isIllegalImportByPackagesAndClassNames(String importText) {
196 boolean result = false;
197 for (String element : illegalPkgs) {
198 if (importText.startsWith(element + ".")) {
199 result = true;
200 break;
201 }
202 }
203 if (!result && illegalClasses != null) {
204 for (String element : illegalClasses) {
205 if (importText.equals(element)) {
206 result = true;
207 break;
208 }
209 }
210 }
211 return result;
212 }
213
214
215
216
217
218
219 private boolean isIllegalImport(String importText) {
220 final boolean result;
221 if (regexp) {
222 result = isIllegalImportByRegularExpressions(importText);
223 }
224 else {
225 result = isIllegalImportByPackagesAndClassNames(importText);
226 }
227 return result;
228 }
229
230 }