/
IllegalImport.java
82 lines (69 loc) · 2.2 KB
/
IllegalImport.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
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.checkstyle.checks.regexp;
import java.util.Collections;
import java.util.HashSet;
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;
/**
* A simple CheckStyle checker to verify specific import statements are not being used.
*
* @author Sanne Grinovero
*/
public class IllegalImport extends AbstractCheck {
private final HashSet<String> notAllowedImports = new HashSet<>();
private String message = "";
/**
* Set the list of illegal import statements.
*
* @param importStatements array of illegal packages
*/
public void setIllegalClassnames(String[] importStatements) {
Collections.addAll( notAllowedImports, importStatements );
}
public void setMessage(String message) {
if ( message != null ) {
this.message = message;
}
}
@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
}
@Override
public int[] getAcceptableTokens() {
return new int[]{ TokenTypes.IMPORT, TokenTypes.STATIC_IMPORT };
}
@Override
public int[] getRequiredTokens() {
return getAcceptableTokens();
}
@Override
public void visitToken(DetailAST aAST) {
final FullIdent imp;
if ( aAST.getType() == TokenTypes.IMPORT ) {
imp = FullIdent.createFullIdentBelow( aAST );
}
else {
// handle case of static imports of method names
imp = FullIdent.createFullIdent( aAST.getFirstChild().getNextSibling() );
}
final String text = imp.getText();
if ( isIllegalImport( text ) ) {
final String message = buildError( text );
log( aAST.getLineNo(), aAST.getColumnNo(), message, text );
}
}
private String buildError(String importStatement) {
return "Import statement violating a checkstyle rule: " + importStatement + ". " + message;
}
private boolean isIllegalImport(String importString) {
return notAllowedImports.contains( importString );
}
}