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;
21
22 import java.util.BitSet;
23 import java.util.List;
24
25 import javax.annotation.Nullable;
26
27 import org.antlr.v4.runtime.Token;
28
29 import com.puppycrawl.tools.checkstyle.api.DetailAST;
30 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
31 import com.puppycrawl.tools.checkstyle.utils.UnmodifiableCollectionUtil;
32
33
34
35
36
37
38
39
40
41 public final class DetailAstImpl implements DetailAST {
42
43
44 private static final int NOT_INITIALIZED = Integer.MIN_VALUE;
45
46
47 private int lineNo = NOT_INITIALIZED;
48
49 private int columnNo = NOT_INITIALIZED;
50
51
52 private int childCount;
53
54 private DetailAstImpl parent;
55
56 private DetailAstImpl previousSibling;
57
58
59 private DetailAstImpl firstChild;
60
61
62 private DetailAstImpl nextSibling;
63
64
65 private String text;
66
67
68 private int type;
69
70
71
72
73
74 private List<Token> hiddenBefore;
75
76
77
78
79
80 private List<Token> hiddenAfter;
81
82
83
84
85
86 private BitSet branchTokenTypes;
87
88
89
90
91 public DetailAstImpl() {
92
93 }
94
95
96
97
98
99
100 public void initialize(Token token) {
101 text = token.getText();
102 type = token.getType();
103 lineNo = token.getLine();
104 columnNo = token.getCharPositionInLine();
105 }
106
107
108
109
110
111
112
113 public void initialize(int tokenType, String tokenText) {
114 type = tokenType;
115 text = tokenText;
116 }
117
118
119
120
121
122
123
124 public void addPreviousSibling(DetailAST ast) {
125 clearBranchTokenTypes();
126 clearChildCountCache(parent);
127 if (ast != null) {
128
129 final DetailAstImpl previousSiblingNode = previousSibling;
130 final DetailAstImpl astImpl = (DetailAstImpl) ast;
131
132 if (previousSiblingNode != null) {
133 previousSiblingNode.setNextSibling(astImpl);
134 }
135 else if (parent != null) {
136 parent.setFirstChild(astImpl);
137 }
138
139 astImpl.setNextSibling(this);
140 }
141 }
142
143
144
145
146
147
148 public void addNextSibling(DetailAST ast) {
149 clearBranchTokenTypes();
150 clearChildCountCache(parent);
151 if (ast != null) {
152
153 final DetailAstImpl sibling = nextSibling;
154 final DetailAstImpl astImpl = (DetailAstImpl) ast;
155 astImpl.setNextSibling(sibling);
156
157 setNextSibling(astImpl);
158 }
159 }
160
161
162
163
164
165
166 public void addChild(DetailAST child) {
167 clearBranchTokenTypes();
168 clearChildCountCache(this);
169 if (child != null) {
170 final DetailAstImpl astImpl = (DetailAstImpl) child;
171 astImpl.setParent(this);
172 }
173 DetailAST temp = firstChild;
174 if (temp == null) {
175 firstChild = (DetailAstImpl) child;
176 }
177 else {
178 while (temp.getNextSibling() != null) {
179 temp = temp.getNextSibling();
180 }
181
182 ((DetailAstImpl) temp).setNextSibling(child);
183 }
184 }
185
186 @Override
187 public int getChildCount() {
188
189 if (childCount == NOT_INITIALIZED) {
190 childCount = 0;
191 DetailAST child = firstChild;
192
193 while (child != null) {
194 childCount += 1;
195 child = child.getNextSibling();
196 }
197 }
198 return childCount;
199 }
200
201 @Override
202 public int getChildCount(int tokenType) {
203 int count = 0;
204 for (DetailAST ast = firstChild; ast != null; ast = ast.getNextSibling()) {
205 if (ast.getType() == tokenType) {
206 count++;
207 }
208 }
209 return count;
210 }
211
212
213
214
215
216
217 private void setParent(DetailAstImpl parent) {
218 DetailAstImpl instance = this;
219 do {
220 instance.clearBranchTokenTypes();
221 instance.parent = parent;
222 instance = instance.nextSibling;
223 } while (instance != null);
224 }
225
226 @Override
227 public DetailAST getParent() {
228 return parent;
229 }
230
231 @Override
232 public String getText() {
233 return text;
234 }
235
236
237
238
239
240
241 public void setText(String text) {
242 this.text = text;
243 }
244
245 @Override
246 public int getType() {
247 return type;
248 }
249
250
251
252
253
254
255 public void setType(int type) {
256 this.type = type;
257 }
258
259 @Override
260 public int getLineNo() {
261 int resultNo = -1;
262
263 if (lineNo == NOT_INITIALIZED) {
264
265
266 resultNo = findLineNo(firstChild);
267
268 if (resultNo == -1) {
269 resultNo = findLineNo(nextSibling);
270 }
271 }
272 if (resultNo == -1) {
273 resultNo = lineNo;
274 }
275 return resultNo;
276 }
277
278
279
280
281
282
283
284 public void setLineNo(int lineNo) {
285 this.lineNo = lineNo;
286 }
287
288 @Override
289 public int getColumnNo() {
290 int resultNo = -1;
291
292 if (columnNo == NOT_INITIALIZED) {
293
294
295 resultNo = findColumnNo(firstChild);
296
297 if (resultNo == -1) {
298 resultNo = findColumnNo(nextSibling);
299 }
300 }
301 if (resultNo == -1) {
302 resultNo = columnNo;
303 }
304 return resultNo;
305 }
306
307
308
309
310
311
312
313 public void setColumnNo(int columnNo) {
314 this.columnNo = columnNo;
315 }
316
317 @Override
318 public DetailAST getLastChild() {
319 DetailAstImpl ast = firstChild;
320 while (ast != null && ast.nextSibling != null) {
321 ast = ast.nextSibling;
322 }
323 return ast;
324 }
325
326
327
328
329
330
331
332 private static int findColumnNo(DetailAST ast) {
333 int resultNo = -1;
334 DetailAST node = ast;
335 while (node != null) {
336
337 if (TokenUtil.isCommentType(node.getType())) {
338 node = node.getNextSibling();
339 }
340 else {
341 resultNo = node.getColumnNo();
342 break;
343 }
344 }
345 return resultNo;
346 }
347
348
349
350
351
352
353
354 private static int findLineNo(DetailAST ast) {
355 int resultNo = -1;
356 DetailAST node = ast;
357 while (node != null) {
358
359 if (TokenUtil.isCommentType(node.getType())) {
360 node = node.getNextSibling();
361 }
362 else {
363 resultNo = node.getLineNo();
364 break;
365 }
366 }
367 return resultNo;
368 }
369
370
371
372
373
374
375 private BitSet getBranchTokenTypes() {
376
377 if (branchTokenTypes == null) {
378 branchTokenTypes = new BitSet();
379 branchTokenTypes.set(type);
380
381
382 DetailAstImpl child = firstChild;
383 while (child != null) {
384 final BitSet childTypes = child.getBranchTokenTypes();
385 branchTokenTypes.or(childTypes);
386
387 child = child.nextSibling;
388 }
389 }
390 return branchTokenTypes;
391 }
392
393 @Override
394 public boolean branchContains(int tokenType) {
395 return getBranchTokenTypes().get(tokenType);
396 }
397
398 @Override
399 public DetailAST getPreviousSibling() {
400 return previousSibling;
401 }
402
403 @Nullable
404 @Override
405 public DetailAST findFirstToken(int tokenType) {
406 DetailAST returnValue = null;
407 for (DetailAST ast = firstChild; ast != null; ast = ast.getNextSibling()) {
408 if (ast.getType() == tokenType) {
409 returnValue = ast;
410 break;
411 }
412 }
413 return returnValue;
414 }
415
416 @Override
417 public String toString() {
418 return text + "[" + getLineNo() + "x" + getColumnNo() + "]";
419 }
420
421 @Override
422 public DetailAstImpl getNextSibling() {
423 return nextSibling;
424 }
425
426 @Override
427 public DetailAstImpl getFirstChild() {
428 return firstChild;
429 }
430
431 @Override
432 public int getNumberOfChildren() {
433 return getChildCount();
434 }
435
436 @Override
437 public boolean hasChildren() {
438 return firstChild != null;
439 }
440
441
442
443
444
445
446 private static void clearChildCountCache(DetailAstImpl ast) {
447 if (ast != null) {
448 ast.childCount = NOT_INITIALIZED;
449 }
450 }
451
452
453
454
455
456 private void clearBranchTokenTypes() {
457 DetailAstImpl prevParent = parent;
458 while (prevParent != null) {
459 prevParent.branchTokenTypes = null;
460 prevParent = prevParent.parent;
461 }
462 }
463
464
465
466
467
468
469 public void setNextSibling(DetailAST nextSibling) {
470 clearBranchTokenTypes();
471 clearChildCountCache(parent);
472 this.nextSibling = (DetailAstImpl) nextSibling;
473 if (nextSibling != null && parent != null) {
474 ((DetailAstImpl) nextSibling).setParent(parent);
475 }
476 if (nextSibling != null) {
477 ((DetailAstImpl) nextSibling).previousSibling = this;
478 }
479 }
480
481
482
483
484
485
486 public void setFirstChild(DetailAST firstChild) {
487 clearBranchTokenTypes();
488 clearChildCountCache(this);
489 this.firstChild = (DetailAstImpl) firstChild;
490 if (firstChild != null) {
491 ((DetailAstImpl) firstChild).setParent(this);
492 }
493 }
494
495
496
497
498 public void removeChildren() {
499 firstChild = null;
500 }
501
502
503
504
505
506
507
508 public List<Token> getHiddenBefore() {
509 List<Token> returnList = null;
510 if (hiddenBefore != null) {
511 returnList = UnmodifiableCollectionUtil.unmodifiableList(hiddenBefore);
512 }
513 return returnList;
514 }
515
516
517
518
519
520
521
522 public List<Token> getHiddenAfter() {
523 List<Token> returnList = null;
524 if (hiddenAfter != null) {
525 returnList = UnmodifiableCollectionUtil.unmodifiableList(hiddenAfter);
526 }
527 return returnList;
528 }
529
530
531
532
533
534
535 public void setHiddenBefore(List<Token> hiddenBefore) {
536 this.hiddenBefore = UnmodifiableCollectionUtil.unmodifiableList(hiddenBefore);
537 }
538
539
540
541
542
543
544 public void setHiddenAfter(List<Token> hiddenAfter) {
545 this.hiddenAfter = UnmodifiableCollectionUtil.unmodifiableList(hiddenAfter);
546 }
547
548 }