diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java b/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java index dacc09102fd..cbbb8165a32 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java @@ -52,7 +52,12 @@ import com.sun.source.tree.Tree; import com.sun.tools.javac.code.Attribute; import com.sun.tools.javac.code.Symbol.MethodSymbol; +import com.sun.tools.javac.parser.JavaTokenizer; +import com.sun.tools.javac.parser.ScannerFactory; +import com.sun.tools.javac.parser.Tokens.Token; +import com.sun.tools.javac.parser.Tokens.TokenKind; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; +import java.nio.CharBuffer; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; @@ -73,19 +78,21 @@ public final class Inliner extends BugChecker public static final String FINDING_TAG = "JavaInlineMe"; - private static final Splitter PACKAGE_SPLITTER = Splitter.on('.'); - static final String PREFIX_FLAG = "InlineMe:Prefix"; + static final String SKIP_COMMENTS_FLAG = "InlineMe:SkipInliningsWithComments"; - private static final String INLINE_ME = "InlineMe"; + private static final Splitter PACKAGE_SPLITTER = Splitter.on('.'); + private static final String INLINE_ME = "InlineMe"; private static final String VALIDATION_DISABLED = "InlineMeValidationDisabled"; private final ImmutableSet apiPrefixes; + private final boolean skipCallsitesWithComments; public Inliner(ErrorProneFlags flags) { this.apiPrefixes = ImmutableSet.copyOf(flags.getSet(PREFIX_FLAG).orElse(ImmutableSet.of())); + this.skipCallsitesWithComments = flags.getBoolean(SKIP_COMMENTS_FLAG).orElse(true); } // TODO(b/163596864): Add support for inlining fields @@ -149,6 +156,10 @@ private Description match( return Description.NO_MATCH; } + if (skipCallsitesWithComments && stringContainsComments(state.getSourceForNode(tree), state)) { + return Description.NO_MATCH; + } + Attribute.Compound inlineMe = symbol.getRawAttributes().stream() .filter(a -> a.type.tsym.getSimpleName().contentEquals(INLINE_ME)) @@ -257,6 +268,20 @@ private Description match( return describe(tree, fix, api); } + // Implementation borrowed from Refaster's comment-checking code. + private static boolean stringContainsComments(String source, VisitorState state) { + JavaTokenizer tokenizer = + new JavaTokenizer(ScannerFactory.instance(state.context), CharBuffer.wrap(source)) {}; + for (Token token = tokenizer.readToken(); + token.kind != TokenKind.EOF; + token = tokenizer.readToken()) { + if (token.comments != null && !token.comments.isEmpty()) { + return true; + } + } + return false; + } + private static ImmutableList getStrings(Attribute.Compound attribute, String name) { return getValue(attribute, name) .map(MoreAnnotations::asStrings)