52
52
import com .sun .source .tree .Tree ;
53
53
import com .sun .tools .javac .code .Attribute ;
54
54
import com .sun .tools .javac .code .Symbol .MethodSymbol ;
55
+ import com .sun .tools .javac .parser .JavaTokenizer ;
56
+ import com .sun .tools .javac .parser .ScannerFactory ;
57
+ import com .sun .tools .javac .parser .Tokens .Token ;
58
+ import com .sun .tools .javac .parser .Tokens .TokenKind ;
55
59
import com .sun .tools .javac .util .JCDiagnostic .DiagnosticPosition ;
60
+ import java .nio .CharBuffer ;
56
61
import java .util .HashMap ;
57
62
import java .util .Map ;
58
63
import java .util .regex .Matcher ;
@@ -73,19 +78,21 @@ public final class Inliner extends BugChecker
73
78
74
79
public static final String FINDING_TAG = "JavaInlineMe" ;
75
80
76
- private static final Splitter PACKAGE_SPLITTER = Splitter .on ('.' );
77
-
78
81
static final String PREFIX_FLAG = "InlineMe:Prefix" ;
82
+ static final String SKIP_COMMENTS_FLAG = "InlineMe:SkipInliningsWithComments" ;
79
83
80
- private static final String INLINE_ME = "InlineMe" ;
84
+ private static final Splitter PACKAGE_SPLITTER = Splitter . on ( '.' ) ;
81
85
86
+ private static final String INLINE_ME = "InlineMe" ;
82
87
private static final String VALIDATION_DISABLED = "InlineMeValidationDisabled" ;
83
88
84
89
private final ImmutableSet <String > apiPrefixes ;
90
+ private final boolean skipCallsitesWithComments ;
85
91
86
92
public Inliner (ErrorProneFlags flags ) {
87
93
this .apiPrefixes =
88
94
ImmutableSet .copyOf (flags .getSet (PREFIX_FLAG ).orElse (ImmutableSet .<String >of ()));
95
+ this .skipCallsitesWithComments = flags .getBoolean (SKIP_COMMENTS_FLAG ).orElse (true );
89
96
}
90
97
91
98
// TODO(b/163596864): Add support for inlining fields
@@ -149,6 +156,10 @@ private Description match(
149
156
return Description .NO_MATCH ;
150
157
}
151
158
159
+ if (skipCallsitesWithComments && stringContainsComments (state .getSourceForNode (tree ), state )) {
160
+ return Description .NO_MATCH ;
161
+ }
162
+
152
163
Attribute .Compound inlineMe =
153
164
symbol .getRawAttributes ().stream ()
154
165
.filter (a -> a .type .tsym .getSimpleName ().contentEquals (INLINE_ME ))
@@ -257,6 +268,20 @@ private Description match(
257
268
return describe (tree , fix , api );
258
269
}
259
270
271
+ // Implementation borrowed from Refaster's comment-checking code.
272
+ private static boolean stringContainsComments (String source , VisitorState state ) {
273
+ JavaTokenizer tokenizer =
274
+ new JavaTokenizer (ScannerFactory .instance (state .context ), CharBuffer .wrap (source )) {};
275
+ for (Token token = tokenizer .readToken ();
276
+ token .kind != TokenKind .EOF ;
277
+ token = tokenizer .readToken ()) {
278
+ if (token .comments != null && !token .comments .isEmpty ()) {
279
+ return true ;
280
+ }
281
+ }
282
+ return false ;
283
+ }
284
+
260
285
private static ImmutableList <String > getStrings (Attribute .Compound attribute , String name ) {
261
286
return getValue (attribute , name )
262
287
.map (MoreAnnotations ::asStrings )
0 commit comments