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.coding;
21
22 import java.util.ArrayDeque;
23 import java.util.BitSet;
24 import java.util.Deque;
25 import java.util.HashSet;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.stream.Collectors;
30
31 import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
32 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
33 import com.puppycrawl.tools.checkstyle.api.DetailAST;
34 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
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 @FileStatefulCheck
94 public final class ModifiedControlVariableCheck extends AbstractCheck {
95
96
97
98
99
100 public static final String MSG_KEY = "modified.control.variable";
101
102
103
104
105 private static final String ILLEGAL_TYPE_OF_TOKEN = "Illegal type of token: ";
106
107
108 private static final BitSet MUTATION_OPERATIONS = TokenUtil.asBitSet(
109 TokenTypes.POST_INC,
110 TokenTypes.POST_DEC,
111 TokenTypes.DEC,
112 TokenTypes.INC,
113 TokenTypes.ASSIGN);
114
115
116 private final Deque<Deque<String>> variableStack = new ArrayDeque<>();
117
118
119
120
121
122
123 private boolean skipEnhancedForLoopVariable;
124
125
126
127
128
129
130
131
132
133 public void setSkipEnhancedForLoopVariable(boolean skipEnhancedForLoopVariable) {
134 this.skipEnhancedForLoopVariable = skipEnhancedForLoopVariable;
135 }
136
137 @Override
138 public int[] getDefaultTokens() {
139 return getRequiredTokens();
140 }
141
142 @Override
143 public int[] getRequiredTokens() {
144 return new int[] {
145 TokenTypes.OBJBLOCK,
146 TokenTypes.LITERAL_FOR,
147 TokenTypes.FOR_ITERATOR,
148 TokenTypes.FOR_EACH_CLAUSE,
149 TokenTypes.ASSIGN,
150 TokenTypes.PLUS_ASSIGN,
151 TokenTypes.MINUS_ASSIGN,
152 TokenTypes.STAR_ASSIGN,
153 TokenTypes.DIV_ASSIGN,
154 TokenTypes.MOD_ASSIGN,
155 TokenTypes.SR_ASSIGN,
156 TokenTypes.BSR_ASSIGN,
157 TokenTypes.SL_ASSIGN,
158 TokenTypes.BAND_ASSIGN,
159 TokenTypes.BXOR_ASSIGN,
160 TokenTypes.BOR_ASSIGN,
161 TokenTypes.INC,
162 TokenTypes.POST_INC,
163 TokenTypes.DEC,
164 TokenTypes.POST_DEC,
165 };
166 }
167
168 @Override
169 public int[] getAcceptableTokens() {
170 return getRequiredTokens();
171 }
172
173 @Override
174 public void beginTree(DetailAST rootAST) {
175
176 variableStack.clear();
177 }
178
179 @Override
180 public void visitToken(DetailAST ast) {
181 switch (ast.getType()) {
182 case TokenTypes.OBJBLOCK:
183 enterBlock();
184 break;
185 case TokenTypes.LITERAL_FOR:
186 case TokenTypes.FOR_ITERATOR:
187 case TokenTypes.FOR_EACH_CLAUSE:
188
189 break;
190 case TokenTypes.ASSIGN:
191 case TokenTypes.PLUS_ASSIGN:
192 case TokenTypes.MINUS_ASSIGN:
193 case TokenTypes.STAR_ASSIGN:
194 case TokenTypes.DIV_ASSIGN:
195 case TokenTypes.MOD_ASSIGN:
196 case TokenTypes.SR_ASSIGN:
197 case TokenTypes.BSR_ASSIGN:
198 case TokenTypes.SL_ASSIGN:
199 case TokenTypes.BAND_ASSIGN:
200 case TokenTypes.BXOR_ASSIGN:
201 case TokenTypes.BOR_ASSIGN:
202 case TokenTypes.INC:
203 case TokenTypes.POST_INC:
204 case TokenTypes.DEC:
205 case TokenTypes.POST_DEC:
206 checkIdent(ast);
207 break;
208 default:
209 throw new IllegalStateException(ILLEGAL_TYPE_OF_TOKEN + ast);
210 }
211 }
212
213 @Override
214 public void leaveToken(DetailAST ast) {
215 switch (ast.getType()) {
216 case TokenTypes.FOR_ITERATOR:
217 leaveForIter(ast.getParent());
218 break;
219 case TokenTypes.FOR_EACH_CLAUSE:
220 if (!skipEnhancedForLoopVariable) {
221 final DetailAST paramDef = ast.findFirstToken(TokenTypes.VARIABLE_DEF);
222 leaveForEach(paramDef);
223 }
224 break;
225 case TokenTypes.LITERAL_FOR:
226 leaveForDef(ast);
227 break;
228 case TokenTypes.OBJBLOCK:
229 exitBlock();
230 break;
231 case TokenTypes.ASSIGN:
232 case TokenTypes.PLUS_ASSIGN:
233 case TokenTypes.MINUS_ASSIGN:
234 case TokenTypes.STAR_ASSIGN:
235 case TokenTypes.DIV_ASSIGN:
236 case TokenTypes.MOD_ASSIGN:
237 case TokenTypes.SR_ASSIGN:
238 case TokenTypes.BSR_ASSIGN:
239 case TokenTypes.SL_ASSIGN:
240 case TokenTypes.BAND_ASSIGN:
241 case TokenTypes.BXOR_ASSIGN:
242 case TokenTypes.BOR_ASSIGN:
243 case TokenTypes.INC:
244 case TokenTypes.POST_INC:
245 case TokenTypes.DEC:
246 case TokenTypes.POST_DEC:
247
248 break;
249 default:
250 throw new IllegalStateException(ILLEGAL_TYPE_OF_TOKEN + ast);
251 }
252 }
253
254
255
256
257 private void enterBlock() {
258 variableStack.push(new ArrayDeque<>());
259 }
260
261
262
263
264 private void exitBlock() {
265 variableStack.pop();
266 }
267
268
269
270
271
272
273 private Deque<String> getCurrentVariables() {
274 return variableStack.peek();
275 }
276
277
278
279
280
281
282 private void checkIdent(DetailAST ast) {
283 final Deque<String> currentVariables = getCurrentVariables();
284 final DetailAST identAST = ast.getFirstChild();
285
286 if (identAST != null && identAST.getType() == TokenTypes.IDENT
287 && currentVariables.contains(identAST.getText())) {
288 log(ast, MSG_KEY, identAST.getText());
289 }
290 }
291
292
293
294
295
296
297 private void leaveForIter(DetailAST ast) {
298 final Set<String> variablesToPutInScope = getVariablesManagedByForLoop(ast);
299 for (String variableName : variablesToPutInScope) {
300 getCurrentVariables().push(variableName);
301 }
302 }
303
304
305
306
307
308
309
310
311 private static Set<String> getVariablesManagedByForLoop(DetailAST ast) {
312 final Set<String> initializedVariables = getForInitVariables(ast);
313 final Set<String> iteratingVariables = getForIteratorVariables(ast);
314 return initializedVariables.stream().filter(iteratingVariables::contains)
315 .collect(Collectors.toUnmodifiableSet());
316 }
317
318
319
320
321
322
323 private void leaveForEach(DetailAST paramDef) {
324
325
326 final boolean isRecordPattern = paramDef == null;
327
328 if (!isRecordPattern) {
329 final DetailAST paramName = paramDef.findFirstToken(TokenTypes.IDENT);
330 getCurrentVariables().push(paramName.getText());
331 }
332 }
333
334
335
336
337
338
339 private void leaveForDef(DetailAST ast) {
340 final DetailAST forInitAST = ast.findFirstToken(TokenTypes.FOR_INIT);
341 if (forInitAST == null) {
342 final Deque<String> currentVariables = getCurrentVariables();
343 if (!skipEnhancedForLoopVariable && !currentVariables.isEmpty()) {
344
345 currentVariables.pop();
346 }
347 }
348 else {
349 final Set<String> variablesManagedByForLoop = getVariablesManagedByForLoop(ast);
350 popCurrentVariables(variablesManagedByForLoop.size());
351 }
352 }
353
354
355
356
357
358
359 private void popCurrentVariables(int count) {
360 for (int i = 0; i < count; i++) {
361 getCurrentVariables().pop();
362 }
363 }
364
365
366
367
368
369
370
371 private static Set<String> getForInitVariables(DetailAST ast) {
372 final Set<String> initializedVariables = new HashSet<>();
373 final DetailAST forInitAST = ast.findFirstToken(TokenTypes.FOR_INIT);
374
375 for (DetailAST parameterDefAST = forInitAST.findFirstToken(TokenTypes.VARIABLE_DEF);
376 parameterDefAST != null;
377 parameterDefAST = parameterDefAST.getNextSibling()) {
378 if (parameterDefAST.getType() == TokenTypes.VARIABLE_DEF) {
379 final DetailAST param =
380 parameterDefAST.findFirstToken(TokenTypes.IDENT);
381
382 initializedVariables.add(param.getText());
383 }
384 }
385 return initializedVariables;
386 }
387
388
389
390
391
392
393
394 private static Set<String> getForIteratorVariables(DetailAST ast) {
395 final Set<String> iteratorVariables = new HashSet<>();
396 final DetailAST forIteratorAST = ast.findFirstToken(TokenTypes.FOR_ITERATOR);
397 final DetailAST forUpdateListAST = forIteratorAST.findFirstToken(TokenTypes.ELIST);
398
399 findChildrenOfExpressionType(forUpdateListAST).stream()
400 .filter(iteratingExpressionAST -> {
401 return MUTATION_OPERATIONS.get(iteratingExpressionAST.getType());
402 }).forEach(iteratingExpressionAST -> {
403 final DetailAST oneVariableOperatorChild = iteratingExpressionAST.getFirstChild();
404 iteratorVariables.add(oneVariableOperatorChild.getText());
405 });
406
407 return iteratorVariables;
408 }
409
410
411
412
413
414
415
416 private static List<DetailAST> findChildrenOfExpressionType(DetailAST ast) {
417 final List<DetailAST> foundExpressions = new LinkedList<>();
418 if (ast != null) {
419 for (DetailAST iteratingExpressionAST = ast.findFirstToken(TokenTypes.EXPR);
420 iteratingExpressionAST != null;
421 iteratingExpressionAST = iteratingExpressionAST.getNextSibling()) {
422 if (iteratingExpressionAST.getType() == TokenTypes.EXPR) {
423 foundExpressions.add(iteratingExpressionAST.getFirstChild());
424 }
425 }
426 }
427 return foundExpressions;
428 }
429
430 }