Skip to content
This repository was archived by the owner on May 12, 2025. It is now read-only.

Commit 6a77148

Browse files
committed
Set the correct MethodType on names of method invocation and declarations.
1 parent c24a822 commit 6a77148

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public J visitArrayAccessExpression(KtArrayAccessExpression expression, Executio
194194
}
195195

196196
JContainer<Expression> args = JContainer.build(Space.EMPTY, expressions, markers);
197-
return new J.MethodInvocation(
197+
return mapType(new J.MethodInvocation(
198198
randomId(),
199199
prefix(expression),
200200
markers,
@@ -203,7 +203,7 @@ public J visitArrayAccessExpression(KtArrayAccessExpression expression, Executio
203203
name,
204204
args,
205205
type
206-
);
206+
));
207207
}
208208

209209
@Override
@@ -2370,7 +2370,7 @@ public J visitDotQualifiedExpression(KtDotQualifiedExpression expression, Execut
23702370
padLeft(suffix(expression.getReceiverExpression()), (J.Identifier) pt.getClazz()),
23712371
pt.getType()
23722372
));
2373-
return pt.withClazz(newName).withPrefix(prefix);
2373+
return mapType(pt.withClazz(newName).withPrefix(prefix));
23742374
} else if (j instanceof J.MethodInvocation) {
23752375
J.MethodInvocation m = (J.MethodInvocation) j;
23762376
return m.getPadding().withSelect(padRight(receiver, suffix(expression.getReceiverExpression())))
@@ -3307,7 +3307,7 @@ private J.MethodInvocation mapFunctionCall(KtBinaryExpression expression, Execut
33073307
JContainer<Expression> args = JContainer.build(Space.EMPTY, expressions, paramMarkers);
33083308
JavaType.Method methodType = methodInvocationType(expression);
33093309

3310-
return new J.MethodInvocation(
3310+
return mapType(new J.MethodInvocation(
33113311
randomId(),
33123312
prefix(expression),
33133313
markers,
@@ -3316,7 +3316,7 @@ private J.MethodInvocation mapFunctionCall(KtBinaryExpression expression, Execut
33163316
name,
33173317
args,
33183318
methodType
3319-
);
3319+
));
33203320
}
33213321

33223322
private J.ControlParentheses<Expression> buildIfCondition(KtIfExpression expression) {
@@ -3482,19 +3482,11 @@ private J.FieldAccess mapType(J.FieldAccess tree) {
34823482
}
34833483

34843484
private J.MethodDeclaration mapType(J.MethodDeclaration tree) {
3485-
J.MethodDeclaration m = tree;
3486-
if (!(m.getName().getType() instanceof JavaType.Method)) {
3487-
m = m.withName(m.getName().withType(m.getMethodType()));
3488-
}
3489-
return m;
3485+
return tree.withName(tree.getName().withType(tree.getMethodType()));
34903486
}
34913487

34923488
private J.MethodInvocation mapType(J.MethodInvocation tree) {
3493-
J.MethodInvocation m = tree;
3494-
if (!(m.getName().getType() instanceof JavaType.Method)) {
3495-
m = m.withName(m.getName().withType(m.getMethodType()));
3496-
}
3497-
return m;
3489+
return tree.withName(tree.getName().withType(tree.getMethodType()));
34983490
}
34993491

35003492
private J.NewClass mapType(J.NewClass tree) {

src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ void implicitInvoke() {
450450
""", spec -> spec.afterRecipe(cu -> {
451451
AtomicBoolean found = new AtomicBoolean(false);
452452
new KotlinIsoVisitor<AtomicBoolean>() {
453-
final MethodMatcher matcher = new MethodMatcher("kotlin.Function1 invoke(..)");
453+
final MethodMatcher matcher = new MethodMatcher("kotlin.Function1 block(..)");
454454
@Override
455455
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
456456
if (matcher.matches(method)) {
457-
assertThat(method.getMethodType().toString()).isEqualTo("kotlin.Function1<kotlin.collections.Collection<kotlin.Any>, kotlin.Unit>{name=invoke,return=kotlin.Unit,parameters=[kotlin.collections.Collection<kotlin.Any>]}");
457+
assertThat(method.getMethodType().toString()).isEqualTo("kotlin.Function1<kotlin.collections.Collection<kotlin.Any>, kotlin.Unit>{name=block,return=kotlin.Unit,parameters=[kotlin.collections.Collection<kotlin.Any>]}");
458458
found.set(true);
459459
}
460460
return super.visitMethodInvocation(method, atomicBoolean);
@@ -1275,5 +1275,28 @@ public J.Annotation visitAnnotation(J.Annotation annotation, Integer integer) {
12751275
)
12761276
);
12771277
}
1278+
1279+
@Issue("https://github.com/openrewrite/rewrite-kotlin/issues/506")
1280+
@Test
1281+
void methodTypeOnMethodInvocation() {
1282+
//noinspection RemoveRedundantBackticks
1283+
rewriteRun(
1284+
kotlin(
1285+
"val arr = listOf(1, 2, 3)",
1286+
spec -> spec.afterRecipe(cu -> {
1287+
AtomicBoolean found = new AtomicBoolean(false);
1288+
new KotlinIsoVisitor<Integer>() {
1289+
@Override
1290+
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer integer) {
1291+
assertThat(method.getName().getType()).isEqualTo(method.getMethodType());
1292+
found.set(true);
1293+
return super.visitMethodInvocation(method, integer);
1294+
}
1295+
}.visit(cu, 0);
1296+
assertThat(found.get()).isTrue();
1297+
})
1298+
)
1299+
);
1300+
}
12781301
}
12791302
}

0 commit comments

Comments
 (0)