27
27
import com .sun .tools .javac .parser .Tokens .TokenKind ;
28
28
import com .sun .tools .javac .parser .UnicodeReader ;
29
29
import com .sun .tools .javac .util .Context ;
30
+ import com .sun .tools .javac .util .Log ;
30
31
import com .sun .tools .javac .util .Position .LineMap ;
31
32
32
33
/** A utility for tokenizing and preserving comments. */
33
34
public class ErrorProneTokens {
34
35
private final int offset ;
35
36
private final CommentSavingTokenizer commentSavingTokenizer ;
36
37
private final ScannerFactory scannerFactory ;
38
+ private final Log log ;
37
39
38
40
public ErrorProneTokens (String source , Context context ) {
39
41
this (source , 0 , context );
@@ -42,6 +44,7 @@ public ErrorProneTokens(String source, Context context) {
42
44
public ErrorProneTokens (String source , int offset , Context context ) {
43
45
this .offset = offset ;
44
46
scannerFactory = ScannerFactory .instance (context );
47
+ log = Log .instance (context );
45
48
char [] buffer = source == null ? new char [] {} : source .toCharArray ();
46
49
commentSavingTokenizer = new CommentSavingTokenizer (scannerFactory , buffer , buffer .length );
47
50
}
@@ -51,13 +54,18 @@ public LineMap getLineMap() {
51
54
}
52
55
53
56
public ImmutableList <ErrorProneToken > getTokens () {
54
- Scanner scanner = new AccessibleScanner (scannerFactory , commentSavingTokenizer );
55
- ImmutableList .Builder <ErrorProneToken > tokens = ImmutableList .builder ();
56
- do {
57
- scanner .nextToken ();
58
- tokens .add (new ErrorProneToken (scanner .token (), offset ));
59
- } while (scanner .token ().kind != TokenKind .EOF );
60
- return tokens .build ();
57
+ Log .DiagnosticHandler diagHandler = new Log .DiscardDiagnosticHandler (log );
58
+ try {
59
+ Scanner scanner = new AccessibleScanner (scannerFactory , commentSavingTokenizer );
60
+ ImmutableList .Builder <ErrorProneToken > tokens = ImmutableList .builder ();
61
+ do {
62
+ scanner .nextToken ();
63
+ tokens .add (new ErrorProneToken (scanner .token (), offset ));
64
+ } while (scanner .token ().kind != TokenKind .EOF );
65
+ return tokens .build ();
66
+ } finally {
67
+ log .popDiagnosticHandler (diagHandler );
68
+ }
61
69
}
62
70
63
71
/** Returns the tokens for the given source text, including comments. */
0 commit comments