2424import com .sun .tools .javac .parser .Tokens .Comment .CommentStyle ;
2525import com .sun .tools .javac .parser .Tokens .Token ;
2626import com .sun .tools .javac .parser .Tokens .TokenKind ;
27- import com .sun .tools .javac .parser .UnicodeReader ;
2827import com .sun .tools .javac .util .Context ;
2928import java .util .ArrayList ;
3029import java .util .List ;
@@ -84,15 +83,18 @@ public static List<RawTok> getTokens(String source, Context context, Set<TokenKi
8483 scanner .nextToken ();
8584 Token t = scanner .token ();
8685 if (t .comments != null ) {
87- for (Comment c : Lists .reverse (t .comments )) {
88- if (last < c .getSourcePos (0 )) {
89- tokens .add (new RawTok (null , last , c .getSourcePos (0 )));
86+ // javac stores the comments in reverse declaration order
87+ for (Comment comment : Lists .reverse (t .comments )) {
88+ if (!(comment instanceof CommentWithPosition c )) {
89+ throw new IllegalStateException (
90+ "A bug in JavacTokens: CommentSavingTokenizer makes every comment, but the tokenizer"
91+ + " gave back a " + comment .getClass ().getName ());
9092 }
91- tokens . add ( new RawTok (
92- null ,
93- c . getSourcePos ( 0 ),
94- c . getSourcePos ( 0 ) + c . getText (). length ()));
95- last = c .getSourcePos ( 0 ) + c . getText (). length ();
93+ if ( last < c . pos ()) {
94+ tokens . add ( new RawTok ( null , last , c . pos ()));
95+ }
96+ tokens . add ( new RawTok ( null , c . pos (), c . endPos ()));
97+ last = c .endPos ();
9698 }
9799 }
98100 if (stopTokens .contains (t .kind )) {
@@ -113,53 +115,49 @@ public static List<RawTok> getTokens(String source, Context context, Set<TokenKi
113115 return List .copyOf (tokens );
114116 }
115117
116- /** A {@link JavaTokenizer} that saves comments . */
118+ /** A {@link JavaTokenizer} that records where each comment starts and ends . */
117119 static class CommentSavingTokenizer extends JavaTokenizer {
120+ private final char [] buffer ;
121+
118122 CommentSavingTokenizer (ScannerFactory fac , char [] buffer , int length ) {
119123 super (fac , buffer , length );
124+ this .buffer = buffer ;
120125 }
121126
122127 @ Override
123128 protected Comment processComment (int pos , int endPos , CommentStyle style ) {
124- char [] buf = getRawCharactersReflectively (pos , endPos );
125- return new CommentWithTextAndPosition (pos , endPos , new AccessibleReader (fac , buf , buf .length ), style );
126- }
127-
128- private char [] getRawCharactersReflectively (int beginIndex , int endIndex ) {
129- Object instance ;
130- try {
131- instance = JavaTokenizer .class .getDeclaredField ("reader" ).get (this );
132- } catch (ReflectiveOperationException e ) {
133- instance = this ;
134- }
135- try {
136- return (char []) instance .getClass ()
137- .getMethod ("getRawCharacters" , int .class , int .class )
138- .invoke (instance , beginIndex , endIndex );
139- } catch (ReflectiveOperationException e ) {
140- throw new RuntimeException (e .getMessage (), e );
141- }
129+ return new CommentWithPosition (buffer , pos , endPos , style );
142130 }
143131 }
144132
145- /** A {@link Comment} that saves its text and start position. */
146- static class CommentWithTextAndPosition implements Comment {
133+ /**
134+ * A {@link Comment} that records its start and end positions. Its text stays in the lexer's buffer until
135+ * {@link #getText} copies it out.
136+ */
137+ static class CommentWithPosition implements Comment {
147138
139+ private final char [] buffer ;
148140 private final int pos ;
149141 private final int endPos ;
150- private final AccessibleReader reader ;
151142 private final CommentStyle style ;
152143
153- @ Nullable
154- private String text = null ;
155-
156- public CommentWithTextAndPosition (int pos , int endPos , AccessibleReader reader , CommentStyle style ) {
144+ CommentWithPosition (char [] buffer , int pos , int endPos , CommentStyle style ) {
145+ this .buffer = buffer ;
157146 this .pos = pos ;
158147 this .endPos = endPos ;
159- this .reader = reader ;
160148 this .style = style ;
161149 }
162150
151+ /** The start position. */
152+ int pos () {
153+ return pos ;
154+ }
155+
156+ /** The end position. */
157+ int endPos () {
158+ return endPos ;
159+ }
160+
163161 /**
164162 * Returns the source position of the character at index {@code index} in the comment text.
165163 *
@@ -178,13 +176,10 @@ public CommentStyle getStyle() {
178176 return style ;
179177 }
180178
179+ /** The comment as it is in the source, unicode escapes untranslated. */
181180 @ Override
182181 public String getText () {
183- String text = this .text ;
184- if (text == null ) {
185- this .text = text = new String (reader .getRawCharacters ());
186- }
187- return text ;
182+ return new String (buffer , pos , endPos - pos );
188183 }
189184
190185 /**
@@ -209,11 +204,4 @@ protected AccessibleScanner(ScannerFactory fac, JavaTokenizer tokenizer) {
209204 super (fac , tokenizer );
210205 }
211206 }
212-
213- // UnicodeReader(ScannerFactory, char[], int) is package-private
214- static class AccessibleReader extends UnicodeReader {
215- protected AccessibleReader (ScannerFactory fac , char [] buffer , int length ) {
216- super (fac , buffer , length );
217- }
218- }
219207}
0 commit comments