15
15
package com .google .googlejavaformat .java ;
16
16
17
17
import static com .google .common .base .Preconditions .checkArgument ;
18
+ import static com .google .common .collect .ImmutableList .toImmutableList ;
18
19
19
20
import com .google .common .collect .ImmutableList ;
20
- import com .google .common .collect .Lists ;
21
21
import com .sun .tools .javac .parser .JavaTokenizer ;
22
22
import com .sun .tools .javac .parser .Scanner ;
23
23
import com .sun .tools .javac .parser .ScannerFactory ;
24
24
import com .sun .tools .javac .parser .Tokens .Comment ;
25
25
import com .sun .tools .javac .parser .Tokens .Comment .CommentStyle ;
26
26
import com .sun .tools .javac .parser .Tokens .Token ;
27
27
import com .sun .tools .javac .parser .Tokens .TokenKind ;
28
- import com .sun .tools .javac .parser .UnicodeReader ;
29
28
import com .sun .tools .javac .util .Context ;
29
+ import java .util .HashMap ;
30
+ import java .util .Map ;
30
31
import java .util .Set ;
31
32
32
33
/** A wrapper around javac's lexer. */
@@ -79,16 +80,16 @@ public static ImmutableList<RawTok> getTokens(
79
80
}
80
81
ScannerFactory fac = ScannerFactory .instance (context );
81
82
char [] buffer = (source + EOF_COMMENT ).toCharArray ();
82
- Scanner scanner =
83
- new AccessibleScanner (fac , new CommentSavingTokenizer ( fac , buffer , buffer . length ) );
83
+ CommentSavingTokenizer tokenizer = new CommentSavingTokenizer ( fac , buffer , buffer . length );
84
+ Scanner scanner = new AccessibleScanner (fac , tokenizer );
84
85
ImmutableList .Builder <RawTok > tokens = ImmutableList .builder ();
85
86
int end = source .length ();
86
87
int last = 0 ;
87
88
do {
88
89
scanner .nextToken ();
89
90
Token t = scanner .token ();
90
91
if (t .comments != null ) {
91
- for (Comment c : Lists . reverse ( t .comments )) {
92
+ for (CommentWithTextAndPosition c : getComments ( t , tokenizer .comments () )) {
92
93
if (last < c .getSourcePos (0 )) {
93
94
tokens .add (new RawTok (null , null , last , c .getSourcePos (0 )));
94
95
}
@@ -120,17 +121,36 @@ public static ImmutableList<RawTok> getTokens(
120
121
return tokens .build ();
121
122
}
122
123
124
+ private static ImmutableList <CommentWithTextAndPosition > getComments (
125
+ Token token , Map <Comment , CommentWithTextAndPosition > comments ) {
126
+ if (token .comments == null ) {
127
+ return ImmutableList .of ();
128
+ }
129
+ // javac stores the comments in reverse declaration order
130
+ return token .comments .stream ().map (comments ::get ).collect (toImmutableList ()).reverse ();
131
+ }
132
+
123
133
/** A {@link JavaTokenizer} that saves comments. */
124
134
static class CommentSavingTokenizer extends JavaTokenizer {
135
+
136
+ private final Map <Comment , CommentWithTextAndPosition > comments = new HashMap <>();
137
+
125
138
CommentSavingTokenizer (ScannerFactory fac , char [] buffer , int length ) {
126
139
super (fac , buffer , length );
127
140
}
128
141
142
+ Map <Comment , CommentWithTextAndPosition > comments () {
143
+ return comments ;
144
+ }
145
+
129
146
@ Override
130
147
protected Comment processComment (int pos , int endPos , CommentStyle style ) {
131
148
char [] buf = getRawCharactersReflectively (pos , endPos );
132
- return new CommentWithTextAndPosition (
133
- pos , endPos , new AccessibleReader (fac , buf , buf .length ), style );
149
+ Comment comment = super .processComment (pos , endPos , style );
150
+ CommentWithTextAndPosition commentWithTextAndPosition =
151
+ new CommentWithTextAndPosition (pos , endPos , new String (buf ));
152
+ comments .put (comment , commentWithTextAndPosition );
153
+ return comment ;
134
154
}
135
155
136
156
private char [] getRawCharactersReflectively (int beginIndex , int endIndex ) {
@@ -153,21 +173,16 @@ private char[] getRawCharactersReflectively(int beginIndex, int endIndex) {
153
173
}
154
174
155
175
/** A {@link Comment} that saves its text and start position. */
156
- static class CommentWithTextAndPosition implements Comment {
176
+ static class CommentWithTextAndPosition {
157
177
158
178
private final int pos ;
159
179
private final int endPos ;
160
- private final AccessibleReader reader ;
161
- private final CommentStyle style ;
162
-
163
- private String text = null ;
180
+ private final String text ;
164
181
165
- public CommentWithTextAndPosition (
166
- int pos , int endPos , AccessibleReader reader , CommentStyle style ) {
182
+ public CommentWithTextAndPosition (int pos , int endPos , String text ) {
167
183
this .pos = pos ;
168
184
this .endPos = endPos ;
169
- this .reader = reader ;
170
- this .style = style ;
185
+ this .text = text ;
171
186
}
172
187
173
188
/**
@@ -176,7 +191,6 @@ public CommentWithTextAndPosition(
176
191
* <p>The handling of javadoc comments in javac has more logic to skip over leading whitespace
177
192
* and '*' characters when indexing into doc comments, but we don't need any of that.
178
193
*/
179
- @ Override
180
194
public int getSourcePos (int index ) {
181
195
checkArgument (
182
196
0 <= index && index < (endPos - pos ),
@@ -186,49 +200,22 @@ public int getSourcePos(int index) {
186
200
return pos + index ;
187
201
}
188
202
189
- @ Override
190
- public CommentStyle getStyle () {
191
- return style ;
192
- }
193
-
194
- @ Override
195
203
public String getText () {
196
- String text = this .text ;
197
- if (text == null ) {
198
- this .text = text = new String (reader .getRawCharacters ());
199
- }
200
204
return text ;
201
205
}
202
206
203
- /**
204
- * We don't care about {@code @deprecated} javadoc tags (see the DepAnn check).
205
- *
206
- * @return false
207
- */
208
- @ Override
209
- public boolean isDeprecated () {
210
- return false ;
211
- }
212
-
213
207
@ Override
214
208
public String toString () {
215
209
return String .format ("Comment: '%s'" , getText ());
216
210
}
217
211
}
218
212
219
- // Scanner(ScannerFactory, JavaTokenizer) is package-private
213
+ // Scanner(ScannerFactory, JavaTokenizer) is protected
220
214
static class AccessibleScanner extends Scanner {
221
215
protected AccessibleScanner (ScannerFactory fac , JavaTokenizer tokenizer ) {
222
216
super (fac , tokenizer );
223
217
}
224
218
}
225
219
226
- // UnicodeReader(ScannerFactory, char[], int) is package-private
227
- static class AccessibleReader extends UnicodeReader {
228
- protected AccessibleReader (ScannerFactory fac , char [] buffer , int length ) {
229
- super (fac , buffer , length );
230
- }
231
- }
232
-
233
220
private JavacTokens () {}
234
221
}
0 commit comments