Skip to content

Commit dc3899e

Browse files
committed
InlineMethodCalls: preserve method select when replacement is bare
When the original invocation has a select (`red.getTotalHits()`) and the replacement template is a bare method call (`getTotalHitsCount()`), prepend the select so the result is `red.getTotalHitsCount()` instead of dropping the receiver. Fixes #7549
1 parent 1684cf4 commit dc3899e

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

rewrite-java/src/main/java/org/openrewrite/java/InlineMethodCalls.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
@Value
3636
public class InlineMethodCalls extends Recipe {
3737
private static final Pattern TEMPLATE_IDENTIFIER = Pattern.compile("#\\{(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*):any\\(.*?\\)}");
38+
private static final Pattern BARE_METHOD_CALL = Pattern.compile("^\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*\\(");
3839

3940
@Option(displayName = "Method pattern",
4041
description = "A method pattern that is used to find matching method invocations.",
@@ -199,6 +200,11 @@ private String createTemplateString(MethodCall original, JavaType.Method methodT
199200
((J.MethodInvocation) original).getSelect() == null &&
200201
replacement.startsWith("this.")) {
201202
templateString = replacement.substring(5);
203+
} else if (original instanceof J.MethodInvocation &&
204+
((J.MethodInvocation) original).getSelect() != null &&
205+
BARE_METHOD_CALL.matcher(replacement).find()) {
206+
// Original had a select but replacement is a bare method call; preserve the select
207+
templateString = "#{this:any()}." + replacement;
202208
} else {
203209
templateString = replacement.replaceAll("\\bthis\\b", "#{this:any()}");
204210
}

rewrite-java/src/test/java/org/openrewrite/java/InlineMethodCallsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,53 @@ void foo(Builder builder) {
508508
);
509509
}
510510

511+
@Test
512+
void preserveSelectWhenReplacementIsBareMethodCall() {
513+
//language=java
514+
rewriteRun(
515+
spec -> spec.recipe(new InlineMethodCalls(
516+
"Lib getTotalHits()",
517+
"getTotalHitsCount()",
518+
null,
519+
null,
520+
null)),
521+
java(
522+
"""
523+
class Lib {
524+
@Deprecated
525+
public int getTotalHits() {
526+
return getTotalHitsCount();
527+
}
528+
529+
public int getTotalHitsCount() {
530+
return 0;
531+
}
532+
533+
static int usage(Lib lib) {
534+
return lib.getTotalHits();
535+
}
536+
}
537+
""",
538+
"""
539+
class Lib {
540+
@Deprecated
541+
public int getTotalHits() {
542+
return getTotalHitsCount();
543+
}
544+
545+
public int getTotalHitsCount() {
546+
return 0;
547+
}
548+
549+
static int usage(Lib lib) {
550+
return lib.getTotalHitsCount();
551+
}
552+
}
553+
"""
554+
)
555+
);
556+
}
557+
511558
@Test
512559
void sameArgumentUsedTwice() {
513560
//language=java

0 commit comments

Comments
 (0)