Skip to content

Commit

Permalink
Stop renameMethodInvocation from renaming any matching arguments with…
Browse files Browse the repository at this point in the history
…in its blast radius.

Fixes external google#1451

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=287828341
  • Loading branch information
graememorgan authored and kmclarnon committed Feb 19, 2020
1 parent 519e817 commit 820e088
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,11 @@ public static SuggestedFix renameMethodInvocation(
} else {
throw malformedMethodInvocationTree(tree);
}
List<ErrorProneToken> tokens = state.getOffsetTokens(startPos, state.getEndPosition(tree));
int endPos =
tree.getArguments().isEmpty()
? state.getEndPosition(tree)
: ((JCTree) tree.getArguments().get(0)).getStartPosition();
List<ErrorProneToken> tokens = state.getOffsetTokens(startPos, endPos);
for (ErrorProneToken token : Lists.reverse(tokens)) {
if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(identifier)) {
return SuggestedFix.replace(token.pos(), token.endPos(), replacement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1314,32 +1314,34 @@ private static class RenameMethodChecker extends BugChecker
implements MethodInvocationTreeMatcher {
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
return describeMatch(tree, SuggestedFixes.renameMethodInvocation(tree, "emptySet", state));
return describeMatch(tree, SuggestedFixes.renameMethodInvocation(tree, "singleton", state));
}
}

@Test
public void renameMethodInvocation() {
BugCheckerRefactoringTestHelper.newInstance(new RenameMethodChecker(), getClass())
.addInputLines(
"Test.java", //
"Test.java",
"import java.util.Collections;",
"class Test {",
" Object foo = Collections.<Integer /* foo */>emptyList();",
" Object bar = Collections.<Integer>/* foo */emptyList();",
" Object baz = Collections.<Integer> emptyList ();",
" int singletonList = 1;",
" Object foo = Collections.<Integer /* foo */>singletonList(singletonList);",
" Object bar = Collections.<Integer>/* foo */singletonList(singletonList);",
" Object baz = Collections.<Integer> singletonList (singletonList);",
" class emptyList {}",
" Object quux = Collections.<emptyList>emptyList();",
" Object quux = Collections.<emptyList>singletonList(null);",
"}")
.addOutputLines(
"Test.java", //
"Test.java",
"import java.util.Collections;",
"class Test {",
" Object foo = Collections.<Integer /* foo */>emptySet();",
" Object bar = Collections.<Integer>/* foo */emptySet();",
" Object baz = Collections.<Integer> emptySet ();",
" int singletonList = 1;",
" Object foo = Collections.<Integer /* foo */>singleton(singletonList);",
" Object bar = Collections.<Integer>/* foo */singleton(singletonList);",
" Object baz = Collections.<Integer> singleton (singletonList);",
" class emptyList {}",
" Object quux = Collections.<emptyList>emptySet();",
" Object quux = Collections.<emptyList>singleton(null);",
"}")
.doTest(TEXT_MATCH);
}
Expand Down

0 comments on commit 820e088

Please sign in to comment.