From b26d6faeb9069bb70b3f61f1d64d53a13dbdad2c Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Wed, 7 Feb 2024 20:36:57 +0100 Subject: [PATCH] Support parsing inner class instantiation via `this` Properly support parsing expressions like `this.Foo()` where `Foo` is the name of an inner class declared in the scope of the type denoted by `this`. --- .../kotlin/internal/KotlinTreeParserVisitor.java | 2 +- .../java/org/openrewrite/kotlin/tree/ThisTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index c7b8b3082..d6e8b0feb 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -2490,7 +2490,7 @@ public J visitDotQualifiedExpression(KtDotQualifiedExpression expression, Execut .withPrefix(prefix); } else if (j instanceof J.NewClass) { J.NewClass n = (J.NewClass) j; - if (receiver instanceof J.FieldAccess || receiver instanceof J.Identifier || receiver instanceof J.NewClass) { + if (receiver instanceof J.FieldAccess || receiver instanceof J.Identifier || receiver instanceof J.NewClass || receiver instanceof K.This) { n = n.withPrefix(prefix); if (n.getClazz() instanceof J.ParameterizedType) { J.ParameterizedType pt = (J.ParameterizedType) n.getClazz(); diff --git a/src/test/java/org/openrewrite/kotlin/tree/ThisTest.java b/src/test/java/org/openrewrite/kotlin/tree/ThisTest.java index a2e02d5d8..244a7c4d0 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/ThisTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/ThisTest.java @@ -47,4 +47,18 @@ override fun iterator(): MutableIterator> { ) ); } + + @Test + void innerClass() { + rewriteRun( + kotlin( + """ + class Foo { + fun bar() = this.Bar() + inner class Bar + } + """ + ) + ); + } }