/
AvoidModifiersForTypesCheck.java
275 lines (250 loc) · 10.4 KB
/
AvoidModifiersForTypesCheck.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
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2015 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.github.sevntu.checkstyle.checks.coding;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import com.github.sevntu.checkstyle.Utils;
import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* Disallow some set of modifiers for Java types specified by regexp. <br>
* <br>
* Only 4 types according to Java Spec: static, final, transient, volatile. <br>
* <br>
* Example: <br>
* "static" modifier for <a
* href="http://ulc.canoo.com/ulccommunity/Contributions/Extensions/GoodPractices.html"
* >ULCComponents</a> <br/>
* Static variables <br/>
* Never keep instances of ULC classes in static variables (ULCIcons neither!).
* They cannot be shared between different sessions. <br>
* So we can disallow "static" modifier for all ULC* components by setting up an
* "forbiddenClassesRegexpStatic" option to "ULC.+" regexp String.
* @author <a href="mailto:Daniil.Yaroslavtsev@gmail.com"> Daniil
* Yaroslavtsev</a>
*/
public class AvoidModifiersForTypesCheck extends Check
{
/**
* The key is pointing to the message text String in
* "messages.properties file".
*/
public static final String MSG_KEY = "avoid.modifiers.for.types";
/**
* Pattern object is used to store the regexp for the names of classes, that
* could not have 'final' modifier.
*/
private Pattern forbiddenClassesRegexpFinal = Pattern.compile("");
/**
* Pattern object is used to store the regexp for the names of classes, that
* could not have 'static' modifier.
*/
private Pattern forbiddenClassesRegexpStatic = Pattern.compile("ULC.+");
/**
* Pattern object is used to store the regexp for the names of classes, that
* could not have 'transient' modifier.
*/
private Pattern forbiddenClassesRegexpTransient = Pattern.compile("");
/**
* Pattern object is used to store the regexp for the names of classes, that
* could not have 'volatile' modifier.
*/
private Pattern forbiddenClassesRegexpVolatile = Pattern.compile("");
/**
* Sets the regexp for the names of classes, that could not have 'static'
* modifier.
* @param aForbiddenClassesRegexpStatic
* String contains the regex to set for the names of classes, that
* could not have 'static' modifier.
*/
public void setForbiddenClassesRegexpStatic(
String forbiddenClassesRegexpStatic)
{
final String regexp = forbiddenClassesRegexpStatic == null ? ""
: forbiddenClassesRegexpStatic;
this.forbiddenClassesRegexpStatic = Pattern.compile(regexp);
}
/**
* Sets the regexp for the names of classes, that could not have 'final'
* modifier.
* @param aForbiddenClassesRegexpFinal
* String contains the regex to set for the names of classes, that
* could not have 'final' modifier.
*/
public void setForbiddenClassesRegexpFinal(
String forbiddenClassesRegexpFinal)
{
final String regexp = forbiddenClassesRegexpFinal == null ? ""
: forbiddenClassesRegexpFinal;
this.forbiddenClassesRegexpFinal = Pattern.compile(regexp);
}
/**
* Sets the regexp for the names of classes, that could not have 'transient'
* modifier.
* @param aForbiddenClassesRegexpTransient
* String contains the regex to set for the names of classes, that
* could not have 'transient' modifier.
*/
public void setForbiddenClassesRegexpTransient(
String forbiddenClassesRegexpTransient)
{
final String regexp = forbiddenClassesRegexpTransient == null ? ""
: forbiddenClassesRegexpTransient;
this.forbiddenClassesRegexpTransient = Pattern.compile(regexp);
}
/**
* Sets the regexp for the names of classes, that could not have 'volatile'
* modifier.
* @param aForbiddenClassesRegexpVolatile
* String contains the regex to set for the names of classes, that
* could not have 'volatile' modifier.
*/
public void setForbiddenClassesRegexpVolatile(
String forbiddenClassesRegexpVolatile)
{
final String regexp = forbiddenClassesRegexpVolatile == null ? ""
: forbiddenClassesRegexpVolatile;
this.forbiddenClassesRegexpVolatile = Pattern.compile(regexp);
}
@Override
public int[] getDefaultTokens()
{
return new int[] {TokenTypes.VARIABLE_DEF };
}
@Override
public void visitToken(DetailAST ast)
{
final String classNameAndPath = getClassNameAndPath(ast);
if (classNameAndPath != null) {
final String className = getClassName(classNameAndPath);
for (int modifierType : getModifiers(ast)) {
switch (modifierType) {
case TokenTypes.LITERAL_STATIC:
if (forbiddenClassesRegexpStatic.matcher(className)
.matches())
{
log(ast, MSG_KEY, className, "static");
}
break;
case TokenTypes.FINAL:
if (forbiddenClassesRegexpFinal.matcher(className)
.matches())
{
log(ast, MSG_KEY, className, "final");
}
break;
case TokenTypes.LITERAL_TRANSIENT:
if (forbiddenClassesRegexpTransient.matcher(className)
.matches())
{
log(ast, MSG_KEY, className, "transient");
}
break;
case TokenTypes.LITERAL_VOLATILE:
if (forbiddenClassesRegexpVolatile.matcher(className)
.matches())
{
log(ast, MSG_KEY, className, "volatile");
}
break;
default:
Utils.reportInvalidToken(modifierType);
break;
}
}
}
}
/**
* Gets the full className of the defined variable.
* @param variableDefNode
* A DetailAST node is related to variable definition (VARIABLE_DEF
* node type).
* @return String contains the className of the defined variable or null if
* the current processed object is an array of primitive types
*/
private static String getClassNameAndPath(DetailAST variableDefNode)
{
String result = null;
final DetailAST type = variableDefNode.findFirstToken(TokenTypes.TYPE);
final DetailAST textWithoutDots = type.findFirstToken(TokenTypes.IDENT);
if (textWithoutDots == null) {
// if there are TokenTypes.DOT nodes in subTree.
final DetailAST parentDotAST = type.findFirstToken(TokenTypes.DOT);
if (parentDotAST != null) {
final FullIdent dottedPathIdent = FullIdent
.createFullIdentBelow(parentDotAST);
final DetailAST nameAST = parentDotAST.getLastChild();
result = dottedPathIdent.getText() + "." + nameAST.getText();
}
}
else { // if subtree doesn`t contain dots.
result = textWithoutDots.getText();
}
return result;
}
/**
* Gets the class name from full (dotted) classPath.
* @param classNameAndPath
* - the full (dotted) classPath. Must not be null.
* @return the name of the class is specified by the current full name&path.
* Guaranteed to not be null if aClassNameAndPath is not null.
*/
private static String getClassName(final String classNameAndPath)
{
return classNameAndPath.replaceAll(".+\\.", "");
}
/**
* Gets the modifiers of the defined variable (final, static, transient or
* volatile).
* @param variableDefAst
* A DeatilAST node is related to the variable definition
* (VARIABLE_DEF type)
* @return List of token types is related to the given variable modifiers.
*/
private static List<Integer> getModifiers(DetailAST variableDefAst)
{
final List<Integer> modifiersList = new LinkedList<Integer>();
final DetailAST modifiersAST = variableDefAst
.findFirstToken(TokenTypes.MODIFIERS);
for (DetailAST modifier : getChildren(modifiersAST)) {
modifiersList.add(modifier.getType());
}
return modifiersList;
}
/**
* Gets all the children which are one level below on the current DetailAST
* parent node.
* @param node
* The parent node.
* @return The list of children one level below on the current parent node.
*/
private static List<DetailAST> getChildren(final DetailAST node)
{
final List<DetailAST> result = new LinkedList<DetailAST>();
DetailAST curNode = node.getFirstChild();
while (curNode != null) {
result.add(curNode);
curNode = curNode.getNextSibling();
}
return result;
}
}