Skip to content

Commit

Permalink
Prevent SuggestedFixes#renameMethod from modifying return type decl…
Browse files Browse the repository at this point in the history
…aration
  • Loading branch information
oxkitsune committed Aug 9, 2023
1 parent 1c1f0f8 commit f85916e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,12 @@ public static SuggestedFix renameMethod(MethodTree tree, String replacement, Vis
int endPos =
tree.getBody() != null ? getStartPosition(tree.getBody()) : state.getEndPosition(tree);
List<ErrorProneToken> methodTokens = state.getOffsetTokens(basePos, endPos);

int returnTypeEndPos = state.getEndPosition(tree.getReturnType());
for (ErrorProneToken token : methodTokens) {
if (token.kind() == TokenKind.IDENTIFIER && token.name().equals(tree.getName())) {
if (token.kind() == TokenKind.IDENTIFIER
&& token.pos() > returnTypeEndPos
&& token.name().equals(tree.getName())) {
return SuggestedFix.replace(token.pos(), token.endPos(), replacement);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,32 @@ public void methodReference() {
"}")
.doTest();
}

@Test
public void methodNameWithMatchingReturnType() {
refactoringHelper
.addInputLines(
"Test.java",
"class Test {",
" private Object Object() {",
" return null;",
" }",
"",
" void call() {",
" Object();",
" }",
"}")
.addOutputLines(
"Test.java",
"class Test {",
" private Object object() {",
" return null;",
" }",
"",
" void call() {",
" object();",
" }",
"}")
.doTest();
}
}

0 comments on commit f85916e

Please sign in to comment.