-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
TreeWalker.java
783 lines (701 loc) · 27.9 KB
/
TreeWalker.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 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;
import java.io.File;
import java.io.Reader;
import java.io.StringReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import antlr.CommonHiddenStreamToken;
import antlr.RecognitionException;
import antlr.Token;
import antlr.TokenStreamException;
import antlr.TokenStreamHiddenTokenFilter;
import antlr.TokenStreamRecognitionException;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.AstFilter;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.Context;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.api.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaLexer;
import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaRecognizer;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
import com.puppycrawl.tools.checkstyle.utils.TokenUtils;
/**
* Responsible for walking an abstract syntax tree and notifying interested
* checks at each each node.
*
* @author Oliver Burn
*/
// -@cs[ClassFanOutComplexity] Number of classes current class relies on exceeds 25.
public final class TreeWalker extends AbstractFileSetCheck implements ExternalResourceHolder {
/** Default distance between tab stops. */
private static final int DEFAULT_TAB_WIDTH = 8;
/** Maps from token name to ordinary checks. */
private final Multimap<String, AbstractCheck> tokenToOrdinaryChecks =
HashMultimap.create();
/** Maps from token name to comment checks. */
private final Multimap<String, AbstractCheck> tokenToCommentChecks =
HashMultimap.create();
/** Registered ordinary checks, that don't use comment nodes. */
private final Set<AbstractCheck> ordinaryChecks = new HashSet<>();
/** Registered comment checks. */
private final Set<AbstractCheck> commentChecks = new HashSet<>();
/** The ast filters. */
private final Set<AstFilter> filters = new HashSet<>();
/** The sorted set of messages. */
private final SortedSet<LocalizedMessage> messages = new TreeSet<>();
/** The distance between tab stops. */
private int tabWidth = DEFAULT_TAB_WIDTH;
/** Class loader to resolve classes with. **/
private ClassLoader classLoader;
/** Context of child components. */
private Context childContext;
/** A factory for creating submodules (i.e. the Checks) */
private ModuleFactory moduleFactory;
/**
* Creates a new {@code TreeWalker} instance.
*/
public TreeWalker() {
setFileExtensions("java");
}
/**
* Sets tab width.
* @param tabWidth the distance between tab stops
*/
public void setTabWidth(int tabWidth) {
this.tabWidth = tabWidth;
}
/**
* Sets cache file.
* @deprecated Use {@link Checker#setCacheFile} instead. It does not do anything now. We just
* keep the setter for transition period to the same option in Checker. The
* method will be completely removed in Checkstyle 8.0. See
* <a href="https://github.com/checkstyle/checkstyle/issues/2883">issue#2883</a>
* @param fileName the cache file
*/
@Deprecated
public void setCacheFile(String fileName) {
// Deprecated
}
/**
* Sets classLoader to load class.
* @param classLoader class loader to resolve classes with.
*/
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
/**
* Sets the module factory for creating child modules (Checks).
* @param moduleFactory the factory
*/
public void setModuleFactory(ModuleFactory moduleFactory) {
this.moduleFactory = moduleFactory;
}
@Override
public void finishLocalSetup() {
final DefaultContext checkContext = new DefaultContext();
checkContext.add("classLoader", classLoader);
checkContext.add("severity", getSeverity());
checkContext.add("tabWidth", String.valueOf(tabWidth));
childContext = checkContext;
}
@Override
public void setupChild(Configuration childConf)
throws CheckstyleException {
final String name = childConf.getName();
final Object module = moduleFactory.createModule(name);
if (module instanceof AbstractCheck || module instanceof AstFilter) {
final AutomaticBean bean = (AutomaticBean) module;
bean.contextualize(childContext);
bean.configure(childConf);
if (module instanceof AbstractCheck) {
final AbstractCheck check = (AbstractCheck) module;
check.init();
registerCheck(check);
}
else {
final AstFilter filter = (AstFilter) module;
filters.add(filter);
}
}
else {
throw new CheckstyleException(
"TreeWalker is not allowed as a parent of " + name
+ " Please review 'Parent Module' section for this Check in web"
+ " documentation if Check is standard.");
}
}
@Override
protected void processFiltered(File file, FileText fileText) throws CheckstyleException {
// check if already checked and passed the file
if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
final String msg = "%s occurred during the analysis of file %s.";
final String fileName = file.getPath();
final FileContents contents = new FileContents(fileText);
try {
if (!ordinaryChecks.isEmpty()
|| !commentChecks.isEmpty()) {
final DetailAST rootAST = parse(contents);
if (!ordinaryChecks.isEmpty()) {
walk(rootAST, contents, AstState.ORDINARY);
}
if (!commentChecks.isEmpty()) {
final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);
walk(astWithComments, contents, AstState.WITH_COMMENTS);
}
}
final SortedSet<LocalizedMessage> filteredMessages = getFilteredMessages(fileName,
contents);
addMessages(filteredMessages);
messages.clear();
}
catch (final TokenStreamRecognitionException tre) {
final String exceptionMsg = String.format(Locale.ROOT, msg,
"TokenStreamRecognitionException", fileName);
throw new CheckstyleException(exceptionMsg, tre);
}
catch (RecognitionException | TokenStreamException ex) {
final String exceptionMsg = String.format(Locale.ROOT, msg,
ex.getClass().getSimpleName(), fileName);
throw new CheckstyleException(exceptionMsg, ex);
}
}
}
/**
* Returns filtered set of {@link LocalizedMessage}.
* @param fileName path to the file
* @param fileContents the contents of the file
* @return filtered set of messages
*/
private SortedSet<LocalizedMessage> getFilteredMessages(String fileName,
FileContents fileContents) {
final SortedSet<LocalizedMessage> result = new TreeSet<>(messages);
for (final LocalizedMessage element : messages) {
final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(fileContents, fileName,
element);
for (final AstFilter filter : filters) {
if (!filter.accept(event)) {
result.remove(element);
break;
}
}
}
return result;
}
/**
* Register a check for a given configuration.
* @param check the check to register
* @throws CheckstyleException if an error occurs
*/
private void registerCheck(AbstractCheck check)
throws CheckstyleException {
validateDefaultTokens(check);
final int[] tokens;
final Set<String> checkTokens = check.getTokenNames();
if (checkTokens.isEmpty()) {
tokens = check.getDefaultTokens();
}
else {
tokens = check.getRequiredTokens();
//register configured tokens
final int[] acceptableTokens = check.getAcceptableTokens();
Arrays.sort(acceptableTokens);
for (String token : checkTokens) {
final int tokenId = TokenUtils.getTokenId(token);
if (Arrays.binarySearch(acceptableTokens, tokenId) >= 0) {
registerCheck(token, check);
}
else {
final String message = String.format(Locale.ROOT, "Token \"%s\" was "
+ "not found in Acceptable tokens list in check %s",
token, check.getClass().getName());
throw new CheckstyleException(message);
}
}
}
for (int element : tokens) {
registerCheck(element, check);
}
if (check.isCommentNodesRequired()) {
commentChecks.add(check);
}
else {
ordinaryChecks.add(check);
}
}
/**
* Register a check for a specified token id.
* @param tokenId the id of the token
* @param check the check to register
* @throws CheckstyleException if Check is misconfigured
*/
private void registerCheck(int tokenId, AbstractCheck check) throws CheckstyleException {
registerCheck(TokenUtils.getTokenName(tokenId), check);
}
/**
* Register a check for a specified token name.
* @param token the name of the token
* @param check the check to register
* @throws CheckstyleException if Check is misconfigured
*/
private void registerCheck(String token, AbstractCheck check) throws CheckstyleException {
if (check.isCommentNodesRequired()) {
tokenToCommentChecks.put(token, check);
}
else if (TokenUtils.isCommentType(token)) {
final String message = String.format(Locale.ROOT, "Check '%s' waits for comment type "
+ "token ('%s') and should override 'isCommentNodesRequired()' "
+ "method to return 'true'", check.getClass().getName(), token);
throw new CheckstyleException(message);
}
else {
tokenToOrdinaryChecks.put(token, check);
}
}
/**
* Validates that check's required tokens are subset of default tokens.
* @param check to validate
* @throws CheckstyleException when validation of default tokens fails
*/
private static void validateDefaultTokens(AbstractCheck check) throws CheckstyleException {
if (check.getRequiredTokens().length != 0) {
final int[] defaultTokens = check.getDefaultTokens();
Arrays.sort(defaultTokens);
for (final int token : check.getRequiredTokens()) {
if (Arrays.binarySearch(defaultTokens, token) < 0) {
final String message = String.format(Locale.ROOT, "Token \"%s\" from required "
+ "tokens was not found in default tokens list in check %s",
token, check.getClass().getName());
throw new CheckstyleException(message);
}
}
}
}
/**
* Initiates the walk of an AST.
* @param ast the root AST
* @param contents the contents of the file the AST was generated from.
* @param astState state of AST.
*/
private void walk(DetailAST ast, FileContents contents,
AstState astState) {
notifyBegin(ast, contents, astState);
// empty files are not flagged by javac, will yield ast == null
if (ast != null) {
processIter(ast, astState);
}
notifyEnd(ast, astState);
}
/**
* Notify checks that we are about to begin walking a tree.
* @param rootAST the root of the tree.
* @param contents the contents of the file the AST was generated from.
* @param astState state of AST.
*/
private void notifyBegin(DetailAST rootAST, FileContents contents,
AstState astState) {
final Set<AbstractCheck> checks;
if (astState == AstState.WITH_COMMENTS) {
checks = commentChecks;
}
else {
checks = ordinaryChecks;
}
for (AbstractCheck check : checks) {
check.setFileContents(contents);
check.clearMessages();
check.beginTree(rootAST);
}
}
/**
* Notify checks that we have finished walking a tree.
* @param rootAST the root of the tree.
* @param astState state of AST.
*/
private void notifyEnd(DetailAST rootAST, AstState astState) {
final Set<AbstractCheck> checks;
if (astState == AstState.WITH_COMMENTS) {
checks = commentChecks;
}
else {
checks = ordinaryChecks;
}
for (AbstractCheck check : checks) {
check.finishTree(rootAST);
messages.addAll(check.getMessages());
}
}
/**
* Notify checks that visiting a node.
* @param ast the node to notify for.
* @param astState state of AST.
*/
private void notifyVisit(DetailAST ast, AstState astState) {
final Collection<AbstractCheck> visitors = getListOfChecks(ast, astState);
if (visitors != null) {
for (AbstractCheck check : visitors) {
check.visitToken(ast);
}
}
}
/**
* Notify checks that leaving a node.
* @param ast
* the node to notify for
* @param astState state of AST.
*/
private void notifyLeave(DetailAST ast, AstState astState) {
final Collection<AbstractCheck> visitors = getListOfChecks(ast, astState);
if (visitors != null) {
for (AbstractCheck check : visitors) {
check.leaveToken(ast);
}
}
}
/**
* Method returns list of checks.
*
* @param ast
* the node to notify for
* @param astState
* state of AST.
* @return list of visitors
*/
private Collection<AbstractCheck> getListOfChecks(DetailAST ast, AstState astState) {
Collection<AbstractCheck> visitors = null;
final String tokenType = TokenUtils.getTokenName(ast.getType());
if (astState == AstState.WITH_COMMENTS) {
if (tokenToCommentChecks.containsKey(tokenType)) {
visitors = tokenToCommentChecks.get(tokenType);
}
}
else {
if (tokenToOrdinaryChecks.containsKey(tokenType)) {
visitors = tokenToOrdinaryChecks.get(tokenType);
}
}
return visitors;
}
/**
* Static helper method to parses a Java source file.
*
* @param contents
* contains the contents of the file
* @return the root of the AST
* @throws TokenStreamException
* if lexing failed
* @throws RecognitionException
* if parsing failed
*/
public static DetailAST parse(FileContents contents)
throws RecognitionException, TokenStreamException {
final String fullText = contents.getText().getFullText().toString();
final Reader reader = new StringReader(fullText);
final GeneratedJavaLexer lexer = new GeneratedJavaLexer(reader);
lexer.setFilename(contents.getFileName());
lexer.setCommentListener(contents);
lexer.setTreatAssertAsKeyword(true);
lexer.setTreatEnumAsKeyword(true);
lexer.setTokenObjectClass("antlr.CommonHiddenStreamToken");
final TokenStreamHiddenTokenFilter filter =
new TokenStreamHiddenTokenFilter(lexer);
filter.hide(TokenTypes.SINGLE_LINE_COMMENT);
filter.hide(TokenTypes.BLOCK_COMMENT_BEGIN);
final GeneratedJavaRecognizer parser =
new GeneratedJavaRecognizer(filter);
parser.setFilename(contents.getFileName());
parser.setASTNodeClass(DetailAST.class.getName());
parser.compilationUnit();
return (DetailAST) parser.getAST();
}
/**
* Parses Java source file. Result AST contains comment nodes.
* @param contents source file content
* @return DetailAST tree
* @throws RecognitionException if parser failed
* @throws TokenStreamException if lexer failed
*/
public static DetailAST parseWithComments(FileContents contents)
throws RecognitionException, TokenStreamException {
return appendHiddenCommentNodes(parse(contents));
}
@Override
public void destroy() {
ordinaryChecks.forEach(AbstractCheck::destroy);
commentChecks.forEach(AbstractCheck::destroy);
super.destroy();
}
@Override
public Set<String> getExternalResourceLocations() {
final Set<String> ordinaryChecksResources = getExternalResourceLocations(ordinaryChecks);
final Set<String> commentChecksResources = getExternalResourceLocations(commentChecks);
final int resultListSize = ordinaryChecksResources.size() + commentChecksResources.size();
final Set<String> resourceLocations = new HashSet<>(resultListSize);
resourceLocations.addAll(ordinaryChecksResources);
resourceLocations.addAll(commentChecksResources);
return resourceLocations;
}
/**
* Returns a set of external configuration resource locations which are used by the checks set.
* @param checks a set of checks.
* @return a set of external configuration resource locations which are used by the checks set.
*/
private static Set<String> getExternalResourceLocations(Set<AbstractCheck> checks) {
final Set<String> externalConfigurationResources = new HashSet<>();
checks.stream().filter(check -> check instanceof ExternalResourceHolder).forEach(check -> {
final Set<String> checkExternalResources =
((ExternalResourceHolder) check).getExternalResourceLocations();
externalConfigurationResources.addAll(checkExternalResources);
});
return externalConfigurationResources;
}
/**
* Processes a node calling interested checks at each node.
* Uses iterative algorithm.
* @param root the root of tree for process
* @param astState state of AST.
*/
private void processIter(DetailAST root, AstState astState) {
DetailAST curNode = root;
while (curNode != null) {
notifyVisit(curNode, astState);
DetailAST toVisit = curNode.getFirstChild();
while (curNode != null && toVisit == null) {
notifyLeave(curNode, astState);
toVisit = curNode.getNextSibling();
if (toVisit == null) {
curNode = curNode.getParent();
}
}
curNode = toVisit;
}
}
/**
* Appends comment nodes to existing AST.
* It traverses each node in AST, looks for hidden comment tokens
* and appends found comment tokens as nodes in AST.
* @param root
* root of AST.
* @return root of AST with comment nodes.
*/
private static DetailAST appendHiddenCommentNodes(DetailAST root) {
DetailAST result = root;
DetailAST curNode = root;
DetailAST lastNode = root;
while (curNode != null) {
if (isPositionGreater(curNode, lastNode)) {
lastNode = curNode;
}
CommonHiddenStreamToken tokenBefore = curNode.getHiddenBefore();
DetailAST currentSibling = curNode;
while (tokenBefore != null) {
final DetailAST newCommentNode =
createCommentAstFromToken(tokenBefore);
currentSibling.addPreviousSibling(newCommentNode);
if (currentSibling == result) {
result = newCommentNode;
}
currentSibling = newCommentNode;
tokenBefore = tokenBefore.getHiddenBefore();
}
DetailAST toVisit = curNode.getFirstChild();
while (curNode != null && toVisit == null) {
toVisit = curNode.getNextSibling();
if (toVisit == null) {
curNode = curNode.getParent();
}
}
curNode = toVisit;
}
if (lastNode != null) {
CommonHiddenStreamToken tokenAfter = lastNode.getHiddenAfter();
DetailAST currentSibling = lastNode;
while (tokenAfter != null) {
final DetailAST newCommentNode =
createCommentAstFromToken(tokenAfter);
currentSibling.addNextSibling(newCommentNode);
currentSibling = newCommentNode;
tokenAfter = tokenAfter.getHiddenAfter();
}
}
return result;
}
/**
* Checks if position of first DetailAST is greater than position of
* second DetailAST. Position is line number and column number in source
* file.
* @param ast1
* first DetailAST node.
* @param ast2
* second DetailAST node.
* @return true if position of ast1 is greater than position of ast2.
*/
private static boolean isPositionGreater(DetailAST ast1, DetailAST ast2) {
final boolean isGreater;
if (ast1.getLineNo() == ast2.getLineNo()) {
isGreater = ast1.getColumnNo() > ast2.getColumnNo();
}
else {
isGreater = ast1.getLineNo() > ast2.getLineNo();
}
return isGreater;
}
/**
* Create comment AST from token. Depending on token type
* SINGLE_LINE_COMMENT or BLOCK_COMMENT_BEGIN is created.
* @param token
* Token object.
* @return DetailAST of comment node.
*/
private static DetailAST createCommentAstFromToken(Token token) {
final DetailAST commentAst;
if (token.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
commentAst = createSlCommentNode(token);
}
else {
commentAst = createBlockCommentNode(token);
}
return commentAst;
}
/**
* Create single-line comment from token.
* @param token
* Token object.
* @return DetailAST with SINGLE_LINE_COMMENT type.
*/
private static DetailAST createSlCommentNode(Token token) {
final DetailAST slComment = new DetailAST();
slComment.setType(TokenTypes.SINGLE_LINE_COMMENT);
slComment.setText("//");
// column counting begins from 0
slComment.setColumnNo(token.getColumn() - 1);
slComment.setLineNo(token.getLine());
final DetailAST slCommentContent = new DetailAST();
slCommentContent.setType(TokenTypes.COMMENT_CONTENT);
// column counting begins from 0
// plus length of '//'
slCommentContent.setColumnNo(token.getColumn() - 1 + 2);
slCommentContent.setLineNo(token.getLine());
slCommentContent.setText(token.getText());
slComment.addChild(slCommentContent);
return slComment;
}
/**
* Create block comment from token.
* @param token
* Token object.
* @return DetailAST with BLOCK_COMMENT type.
*/
private static DetailAST createBlockCommentNode(Token token) {
final DetailAST blockComment = new DetailAST();
blockComment.initialize(TokenTypes.BLOCK_COMMENT_BEGIN, "/*");
// column counting begins from 0
blockComment.setColumnNo(token.getColumn() - 1);
blockComment.setLineNo(token.getLine());
final DetailAST blockCommentContent = new DetailAST();
blockCommentContent.setType(TokenTypes.COMMENT_CONTENT);
// column counting begins from 0
// plus length of '/*'
blockCommentContent.setColumnNo(token.getColumn() - 1 + 2);
blockCommentContent.setLineNo(token.getLine());
blockCommentContent.setText(token.getText());
final DetailAST blockCommentClose = new DetailAST();
blockCommentClose.initialize(TokenTypes.BLOCK_COMMENT_END, "*/");
final Entry<Integer, Integer> linesColumns = countLinesColumns(
token.getText(), token.getLine(), token.getColumn());
blockCommentClose.setLineNo(linesColumns.getKey());
blockCommentClose.setColumnNo(linesColumns.getValue());
blockComment.addChild(blockCommentContent);
blockComment.addChild(blockCommentClose);
return blockComment;
}
/**
* Count lines and columns (in last line) in text.
* @param text
* String.
* @param initialLinesCnt
* initial value of lines counter.
* @param initialColumnsCnt
* initial value of columns counter.
* @return entry(pair), first element is lines counter, second - columns
* counter.
*/
private static Entry<Integer, Integer> countLinesColumns(
String text, int initialLinesCnt, int initialColumnsCnt) {
int lines = initialLinesCnt;
int columns = initialColumnsCnt;
boolean foundCr = false;
for (char c : text.toCharArray()) {
if (c == '\n') {
foundCr = false;
lines++;
columns = 0;
}
else {
if (foundCr) {
foundCr = false;
lines++;
columns = 0;
}
if (c == '\r') {
foundCr = true;
}
columns++;
}
}
if (foundCr) {
lines++;
columns = 0;
}
return new SimpleEntry<>(lines, columns);
}
/**
* State of AST.
* Indicates whether tree contains certain nodes.
*/
private enum AstState {
/**
* Ordinary tree.
*/
ORDINARY,
/**
* AST contains comment nodes.
*/
WITH_COMMENTS
}
}