Skip to content

Commit

Permalink
For InlineMe - avoid rewriting callsites where there are interior com…
Browse files Browse the repository at this point in the history
…ments, with a flag to revert the behavior if you don't care about removing comments.

PiperOrigin-RevId: 405014564
  • Loading branch information
nick-someone authored and Error Prone Team committed Oct 22, 2021
1 parent 2ca990d commit 6551909
Showing 1 changed file with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> apiPrefixes;
private final boolean skipCallsitesWithComments;

public Inliner(ErrorProneFlags flags) {
this.apiPrefixes =
ImmutableSet.copyOf(flags.getSet(PREFIX_FLAG).orElse(ImmutableSet.<String>of()));
this.skipCallsitesWithComments = flags.getBoolean(SKIP_COMMENTS_FLAG).orElse(true);
}

// TODO(b/163596864): Add support for inlining fields
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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<String> getStrings(Attribute.Compound attribute, String name) {
return getValue(attribute, name)
.map(MoreAnnotations::asStrings)
Expand Down

0 comments on commit 6551909

Please sign in to comment.