Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1451: SuggestedFixes.renameMethodInvocation doesn't modify params #1452

Closed
wants to merge 1 commit into from
Closed
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 @@ -586,9 +586,14 @@ public static SuggestedFix renameMethodInvocation(
throw malformedMethodInvocationTree(tree);
}
List<ErrorProneToken> tokens = state.getOffsetTokens(startPos, state.getEndPosition(tree));
int depth = 0;
for (ErrorProneToken token : Lists.reverse(tokens)) {
if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(identifier)) {
if (depth == 0 && token.kind() == TokenKind.IDENTIFIER && token.name().equals(identifier)) {
return SuggestedFix.replace(token.pos(), token.endPos(), replacement);
} else if (token.kind() == Tokens.TokenKind.RPAREN) {
depth++;
} else if (token.kind() == Tokens.TokenKind.LPAREN) {
depth--;
}
}
throw malformedMethodInvocationTree(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,43 @@ public void renameMethodInvocation() {
.doTest(TEXT_MATCH);
}

@BugPattern(
name = "RenameMethodChecker2",
summary = "RenameMethodChecker2",
severity = ERROR,
providesFix = ProvidesFix.REQUIRES_HUMAN_ATTENTION)
private static class RenameMethodChecker2 extends BugChecker
implements MethodInvocationTreeMatcher {
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
return describeMatch(tree, SuggestedFixes.renameMethodInvocation(tree, "singleton", state));
}
}

@Test
public void renameMethodInvocationWithArg() {
BugCheckerRefactoringTestHelper.newInstance(new RenameMethodChecker2(), getClass())
.addInputLines(
"Test.java",
"import java.util.Collections;",
"class Test {",
" Integer singletonList = 1;",
" Object foo = Collections.<Integer /* foo */>singletonList(singletonList);",
" Object bar = Collections.<Integer>/* foo */singletonList(singletonList);",
" Object baz = Collections.<Integer> singletonList (singletonList);",
"}")
.addOutputLines(
"Test.java",
"import java.util.Collections;",
"class Test {",
" Integer singletonList = 1;",
" Object foo = Collections.<Integer /* foo */>singleton(singletonList);",
" Object bar = Collections.<Integer>/* foo */singleton(singletonList);",
" Object baz = Collections.<Integer> singleton (singletonList);",
"}")
.doTest(TEXT_MATCH);
}

/**
* Test checker that raises a diagnostic with the result of {@link SuggestedFixes#qualifyType} on
* new instances.
Expand Down