Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;

import com.google.errorprone.BugPattern;
Expand All @@ -29,7 +30,6 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
Expand Down Expand Up @@ -62,21 +62,24 @@ public Description matchMethodInvocation(MethodInvocationTree build, VisitorStat
if (withinBugChecker(state)) {
return NO_MATCH;
}
ExpressionTree addFix = ASTHelpers.getReceiver(build);
ExpressionTree addFix = getReceiver(build);
if (!ADD_FIX.matches(addFix, state)) {
return NO_MATCH;
}
ExpressionTree buildDescription = ASTHelpers.getReceiver(addFix);
ExpressionTree buildDescription = getReceiver(addFix);
if (!BUILD_DESCRIPTION.matches(buildDescription, state)) {
return NO_MATCH;
}

return describeMatch(
build,
SuggestedFix.replace(
build,
String.format(
"describeMatch(%s, %s)",
getArgument(state, buildDescription), getArgument(state, addFix))));
"%sdescribeMatch(%s, %s)",
getReceiverSourceAndDotOrBlank(buildDescription, state),
getArgumentSource(buildDescription, state),
getArgumentSource(addFix, state))));
}

private static boolean withinBugChecker(VisitorState state) {
Expand All @@ -89,7 +92,12 @@ && getSymbol(t)
.contentEquals(BugChecker.class.getCanonicalName()));
}

private static String getArgument(VisitorState state, ExpressionTree tree) {
private static String getReceiverSourceAndDotOrBlank(ExpressionTree tree, VisitorState state) {
ExpressionTree receiver = getReceiver(tree);
return receiver != null ? state.getSourceForNode(receiver) + "." : "";
}

private static String getArgumentSource(ExpressionTree tree, VisitorState state) {
return state.getSourceForNode(getOnlyElement(((MethodInvocationTree) tree).getArguments()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,36 @@ public void refactoring() {
.doTest();
}

@Test
public void refactoringWithReceiver() {
testHelper
.addInputLines(
"Test.java",
"import com.google.errorprone.bugpatterns.BugChecker;",
"import com.google.errorprone.fixes.Fix;",
"import com.sun.source.tree.Tree;",
"import com.google.errorprone.matchers.Description;",
"class Test extends BugChecker {",
" Description fix(Tree tree, Fix fix) {",
" return this.buildDescription(tree).addFix(fix).build();",
" }",
"}")
.addOutputLines(
"Test.java",
"import com.google.errorprone.bugpatterns.BugChecker;",
"import com.google.errorprone.fixes.Fix;",
"import com.sun.source.tree.Tree;",
"import com.google.errorprone.matchers.Description;",
"class Test extends BugChecker {",
" Description fix(Tree tree, Fix fix) {",
" return this.describeMatch(tree, fix);",
" }",
"}")
.addModules(
"jdk.compiler/com.sun.tools.javac.util", "jdk.compiler/com.sun.tools.javac.tree")
.doTest();
}

@Test
public void noMatchInBugChecker() {
testHelper
Expand Down