2727import com .sun .tools .javac .parser .Tokens .TokenKind ;
2828import com .sun .tools .javac .parser .UnicodeReader ;
2929import com .sun .tools .javac .util .Context ;
30+ import com .sun .tools .javac .util .Log ;
3031import com .sun .tools .javac .util .Position .LineMap ;
3132
3233/** A utility for tokenizing and preserving comments. */
3334public class ErrorProneTokens {
3435 private final int offset ;
3536 private final CommentSavingTokenizer commentSavingTokenizer ;
3637 private final ScannerFactory scannerFactory ;
38+ private final Log log ;
3739
3840 public ErrorProneTokens (String source , Context context ) {
3941 this (source , 0 , context );
@@ -42,6 +44,7 @@ public ErrorProneTokens(String source, Context context) {
4244 public ErrorProneTokens (String source , int offset , Context context ) {
4345 this .offset = offset ;
4446 scannerFactory = ScannerFactory .instance (context );
47+ log = Log .instance (context );
4548 char [] buffer = source == null ? new char [] {} : source .toCharArray ();
4649 commentSavingTokenizer = new CommentSavingTokenizer (scannerFactory , buffer , buffer .length );
4750 }
@@ -51,13 +54,18 @@ public LineMap getLineMap() {
5154 }
5255
5356 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+ }
6169 }
6270
6371 /** Returns the tokens for the given source text, including comments. */
0 commit comments