Skip to content

Conversation

@greg-dennis
Copy link
Contributor

@greg-dennis greg-dennis commented Dec 8, 2025

What's changed?

What's your motivation?

Add unit test showing null Methodinvocation type. This test fails in this repo but the very same test passes when added to the rewrite repo.

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Comment on lines +41 to +79
@Test
void showNullMethodInvocationType() {
//language=kotlin
SourceSpec<?> source = getOnly(
kotlin(
"""
class ExampleTest {
fun myTest() = run {
return 1
}
}
"""
));
Parser parser = KotlinParser.builder().build();
Path sourcePath = parser.sourcePathFromSourceText(source.getDir(), source.getBefore());
ExecutionContext ctx = new InMemoryExecutionContext();
Parser.Input input = Parser.Input.fromString(
sourcePath, source.getBefore(), parser.getCharset(ctx));
SourceFile sourceFile = getOnly(
parser.parseInputs(singleton(input), null, ctx).toList());
new FindMissingTypesVisitor().visit(sourceFile, ctx);
}

private static class FindMissingTypesVisitor extends KotlinIsoVisitor<ExecutionContext> {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,
ExecutionContext ctx) {
assertNotNull(method.getMethodType());
return method;
}
}

private static <T> T getOnly(Iterable<T> iterable) {
Iterator<T> iter = iterable.iterator();
T only = iter.next();
assertFalse(iter.hasNext());
return only;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Test
void showNullMethodInvocationType() {
//language=kotlin
SourceSpec<?> source = getOnly(
kotlin(
"""
class ExampleTest {
fun myTest() = run {
return 1
}
}
"""
));
Parser parser = KotlinParser.builder().build();
Path sourcePath = parser.sourcePathFromSourceText(source.getDir(), source.getBefore());
ExecutionContext ctx = new InMemoryExecutionContext();
Parser.Input input = Parser.Input.fromString(
sourcePath, source.getBefore(), parser.getCharset(ctx));
SourceFile sourceFile = getOnly(
parser.parseInputs(singleton(input), null, ctx).toList());
new FindMissingTypesVisitor().visit(sourceFile, ctx);
}
private static class FindMissingTypesVisitor extends KotlinIsoVisitor<ExecutionContext> {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method,
ExecutionContext ctx) {
assertNotNull(method.getMethodType());
return method;
}
}
private static <T> T getOnly(Iterable<T> iterable) {
Iterator<T> iter = iterable.iterator();
T only = iter.next();
assertFalse(iter.hasNext());
return only;
}
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(KotlinParser.builder());
}
@Test
void showNullMethodInvocationType() {
rewriteRun(
//language=kotlin
kotlin(
"""
class ExampleTest {
fun myTest() = run {
return 1
}
}
"""
));
}

I've simplified it to this and am indeed seeing it fail in rewrite-testing-frameworks but succeed in rewrite

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that simple example will show the symptom, but I found the failure to be more cryptic. The intention of the PR was to reveal the underlying issue: that the type of the run method invocation is null.

Copy link
Contributor Author

@greg-dennis greg-dennis Dec 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Laurens-W, there appears to be other code that "expects" the types of Kotlin method invocations to sometimes be null (example). If that is sometimes "expected" behavior, the following workaround might make sense: openrewrite/rewrite#6409

Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

3 participants