-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
IllegalImportCheck.java
449 lines (427 loc) · 13.8 KB
/
IllegalImportCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.imports;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
/**
* <p>
* Checks for imports from a set of illegal packages.
* </p>
* <p>
* Note: By default, the check rejects all {@code sun.*} packages since programs
* that contain direct calls to the {@code sun.*} packages are
* <a href="https://www.oracle.com/java/technologies/faq-sun-packages.html">
* "not guaranteed to work on all Java-compatible platforms"</a>. To reject other
* packages, set property {@code illegalPkgs} to a list of the illegal packages.
* </p>
* <ul>
* <li>
* Property {@code illegalPkgs} - Specify packages to reject, if <b>regexp</b>
* property is not set, checks if import is the part of package. If <b>regexp</b>
* property is set, then list of packages will be interpreted as regular expressions.
* Note, all properties for match will be used.
* Default value is {@code sun}.
* </li>
* <li>
* Property {@code illegalClasses} - Specify class names to reject, if <b>regexp</b>
* property is not set, checks if import equals class name. If <b>regexp</b>
* property is set, then list of class names will be interpreted as regular expressions.
* Note, all properties for match will be used.
* Default value is {@code {}}.
* </li>
* <li>
* Property {@code regexp} - Control whether the {@code illegalPkgs} and
* {@code illegalClasses} should be interpreted as regular expressions.
* Default value is {@code false}.
* </li>
* </ul>
* <p>
* To configure the check:
* </p>
* <pre>
* <module name="IllegalImport"/>
* </pre>
* <p>
* To configure the check so that it rejects packages {@code java.io.*} and {@code java.sql.*}:
* </p>
* <pre>
* <module name="IllegalImport">
* <property name="illegalPkgs" value="java.io, java.sql"/>
* </module>
* </pre>
* <p>
* The following example shows class with no illegal imports
* </p>
* <pre>
* import java.lang.ArithmeticException;
* import java.util.List;
* import java.util.Enumeration;
* import java.util.Arrays;
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* The following example shows class with two illegal imports
* </p>
* <ul>
* <li>
* <b>java.io.*</b>, illegalPkgs property contains this package
* </li>
* <li>
* <b>java.sql.Connection</b> is inside java.sql package
* </li>
* </ul>
* <pre>
* import java.io.*; // violation
* import java.lang.ArithmeticException;
* import java.sql.Connection; // violation
* import java.util.List;
* import java.util.Enumeration;
* import java.util.Arrays;
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* To configure the check so that it rejects classes {@code java.util.Date} and
* {@code java.sql.Connection}:
* </p>
* <pre>
* <module name="IllegalImport">
* <property name="illegalClasses"
* value="java.util.Date, java.sql.Connection"/>
* </module>
* </pre>
* <p>
* The following example shows class with no illegal imports
* </p>
* <pre>
* import java.io.*;
* import java.lang.ArithmeticException;
* import java.util.List;
* import java.util.Enumeration;
* import java.util.Arrays;
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* The following example shows class with two illegal imports
* </p>
* <ul>
* <li>
* <b>java.sql.Connection</b>, illegalClasses property contains this class
* </li>
* <li>
* <b>java.util.Date</b>, illegalClasses property contains this class
* </li>
* </ul>
* <pre>
* import java.io.*;
* import java.lang.ArithmeticException;
* import java.sql.Connection; // violation
* import java.util.List;
* import java.util.Enumeration;
* import java.util.Arrays;
* import java.util.Date; // violation
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* To configure the check so that it rejects packages not satisfying to regular
* expression {@code java\.util}:
* </p>
* <pre>
* <module name="IllegalImport">
* <property name="regexp" value="true"/>
* <property name="illegalPkgs" value="java\.util"/>
* </module>
* </pre>
* <p>
* The following example shows class with no illegal imports
* </p>
* <pre>
* import java.io.*;
* import java.lang.ArithmeticException;
* import java.sql.Connection;
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* The following example shows class with four illegal imports
* </p>
* <ul>
* <li>
* <b>java.util.List</b>
* </li>
* <li>
* <b>java.util.Enumeration</b>
* </li>
* <li>
* <b>java.util.Arrays</b>
* </li>
* <li>
* <b>java.util.Date</b>
* </li>
* </ul>
* <p>
* All four imports match "java\.util" regular expression
* </p>
* <pre>
* import java.io.*;
* import java.lang.ArithmeticException;
* import java.sql.Connection;
* import java.util.List; // violation
* import java.util.Enumeration; // violation
* import java.util.Arrays; // violation
* import java.util.Date; // violation
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* To configure the check so that it rejects class names not satisfying to regular
* expression {@code ^java\.util\.(List|Arrays)} and {@code ^java\.sql\.Connection}:
* </p>
* <pre>
* <module name="IllegalImport">
* <property name="regexp" value="true"/>
* <property name="illegalClasses"
* value="^java\.util\.(List|Arrays), ^java\.sql\.Connection"/>
* </module>
* </pre>
* <p>
* The following example shows class with no illegal imports
* </p>
* <pre>
* import java.io.*;
* import java.lang.ArithmeticException;
* import java.util.Enumeration;
* import java.util.Date;
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
* <p>
* The following example shows class with three illegal imports
* </p>
* <ul>
* <li>
* <b>java.sql.Connection</b> matches "^java\.sql\.Connection" regular expression
* </li>
* <li>
* <b>java.util.List</b> matches "^java\.util\.(List|Arrays)" regular expression
* </li>
* <li>
* <b>java.util.Arrays</b> matches "^java\.util\.(List|Arrays)" regular expression
* </li>
* </ul>
* <pre>
* import java.io.*;
* import java.lang.ArithmeticException;
* import java.sql.Connection; // violation
* import java.util.List; // violation
* import java.util.Enumeration;
* import java.util.Arrays; // violation
* import java.util.Date;
* import sun.applet.*;
*
* public class InputIllegalImport { }
* </pre>
*
* @since 3.0
*/
@StatelessCheck
public class IllegalImportCheck
extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY = "import.illegal";
/** The compiled regular expressions for packages. */
private final List<Pattern> illegalPkgsRegexps = new ArrayList<>();
/** The compiled regular expressions for classes. */
private final List<Pattern> illegalClassesRegexps = new ArrayList<>();
/**
* Specify packages to reject, if <b>regexp</b> property is not set, checks
* if import is the part of package. If <b>regexp</b> property is set, then
* list of packages will be interpreted as regular expressions.
* Note, all properties for match will be used.
*/
private String[] illegalPkgs;
/**
* Specify class names to reject, if <b>regexp</b> property is not set,
* checks if import equals class name. If <b>regexp</b> property is set,
* then list of class names will be interpreted as regular expressions.
* Note, all properties for match will be used.
*/
private String[] illegalClasses;
/**
* Control whether the {@code illegalPkgs} and {@code illegalClasses}
* should be interpreted as regular expressions.
*/
private boolean regexp;
/**
* Creates a new {@code IllegalImportCheck} instance.
*/
public IllegalImportCheck() {
setIllegalPkgs("sun");
}
/**
* Setter to specify packages to reject, if <b>regexp</b> property is not set,
* checks if import is the part of package. If <b>regexp</b> property is set,
* then list of packages will be interpreted as regular expressions.
* Note, all properties for match will be used.
*
* @param from array of illegal packages
* @noinspection WeakerAccess
*/
public final void setIllegalPkgs(String... from) {
illegalPkgs = from.clone();
illegalPkgsRegexps.clear();
for (String illegalPkg : illegalPkgs) {
illegalPkgsRegexps.add(CommonUtil.createPattern("^" + illegalPkg + "\\..*"));
}
}
/**
* Setter to specify class names to reject, if <b>regexp</b> property is not
* set, checks if import equals class name. If <b>regexp</b> property is set,
* then list of class names will be interpreted as regular expressions.
* Note, all properties for match will be used.
*
* @param from array of illegal classes
*/
public void setIllegalClasses(String... from) {
illegalClasses = from.clone();
for (String illegalClass : illegalClasses) {
illegalClassesRegexps.add(CommonUtil.createPattern(illegalClass));
}
}
/**
* Setter to control whether the {@code illegalPkgs} and {@code illegalClasses}
* should be interpreted as regular expressions.
*
* @param regexp a {@code Boolean} value
*/
public void setRegexp(boolean regexp) {
this.regexp = regexp;
}
@Override
public int[] getDefaultTokens() {
return getRequiredTokens();
}
@Override
public int[] getAcceptableTokens() {
return getRequiredTokens();
}
@Override
public int[] getRequiredTokens() {
return new int[] {TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT};
}
@Override
public void visitToken(DetailAST ast) {
final FullIdent imp;
if (ast.getType() == TokenTypes.IMPORT) {
imp = FullIdent.createFullIdentBelow(ast);
}
else {
imp = FullIdent.createFullIdent(
ast.getFirstChild().getNextSibling());
}
if (isIllegalImport(imp.getText())) {
log(ast,
MSG_KEY,
imp.getText());
}
}
/**
* Checks if an import matches one of the regular expressions
* for illegal packages or illegal class names.
* @param importText the argument of the import keyword
* @return if {@code importText} matches one of the regular expressions
* for illegal packages or illegal class names
*/
private boolean isIllegalImportByRegularExpressions(String importText) {
boolean result = false;
for (Pattern pattern : illegalPkgsRegexps) {
if (pattern.matcher(importText).matches()) {
result = true;
break;
}
}
if (!result) {
for (Pattern pattern : illegalClassesRegexps) {
if (pattern.matcher(importText).matches()) {
result = true;
break;
}
}
}
return result;
}
/**
* Checks if an import is from a package or class name that must not be used.
* @param importText the argument of the import keyword
* @return if {@code importText} contains an illegal package prefix or equals illegal class name
*/
private boolean isIllegalImportByPackagesAndClassNames(String importText) {
boolean result = false;
for (String element : illegalPkgs) {
if (importText.startsWith(element + ".")) {
result = true;
break;
}
}
if (illegalClasses != null) {
for (String element : illegalClasses) {
if (importText.equals(element)) {
result = true;
break;
}
}
}
return result;
}
/**
* Checks if an import is from a package or class name that must not be used.
* @param importText the argument of the import keyword
* @return if {@code importText} is illegal import
*/
private boolean isIllegalImport(String importText) {
final boolean result;
if (regexp) {
result = isIllegalImportByRegularExpressions(importText);
}
else {
result = isIllegalImportByPackagesAndClassNames(importText);
}
return result;
}
}