diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index ec00c5452..62bd1f3ca 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -1658,12 +1658,24 @@ private J.MethodDeclaration visitNamedFunction0(KtNamedFunction function, Execut if (function.getTypeParameterList() != null) { - throw new UnsupportedOperationException("TODO"); - } - if (function.getReceiverTypeReference() != null) { - throw new UnsupportedOperationException("TODO"); + List ktTypeParameters = function.getTypeParameters(); + List> params = new ArrayList<>(ktTypeParameters.size()); + + for (KtTypeParameter ktTypeParameter : ktTypeParameters) { + J.TypeParameter typeParameter = ktTypeParameter.accept(this, data).withPrefix(Space.EMPTY); + params.add(padRight(typeParameter, suffix(ktTypeParameter))); + } + + typeParameters = new J.TypeParameters( + randomId(), + prefix(function.getTypeParameterList()), + Markers.EMPTY, + emptyList(), + params + ); } + List typeParams = function.getTypeParameters(); boolean isOpen = function.hasModifier(KtTokens.OPEN_KEYWORD); @@ -1719,6 +1731,51 @@ private J.MethodDeclaration visitNamedFunction0(KtNamedFunction function, Execut params = JContainer.build(prefix(function.getValueParameterList()), rps, Markers.EMPTY); } + + if (function.getReceiverTypeReference() != null) { + markers = markers.addIfAbsent(new Extension(randomId())); + Expression receiver = convertToExpression(function.getReceiverTypeReference().accept(this, data)).withPrefix(prefix(function.getReceiverTypeReference())); + JRightPadded infixReceiver = JRightPadded.build( + new J.VariableDeclarations.NamedVariable( + randomId(), + Space.EMPTY, + Markers.EMPTY.addIfAbsent(new Extension(randomId())), + new J.Identifier( + randomId(), + Space.EMPTY, + Markers.EMPTY, + emptyList(), + "", + null, + null + ), + emptyList(), + padLeft(Space.EMPTY, receiver), + null + ) + ) + .withAfter(suffix(function.getReceiverTypeReference())); + + J.VariableDeclarations implicitParam = new J.VariableDeclarations( + randomId(), + Space.EMPTY, + Markers.EMPTY.addIfAbsent(new Extension(randomId())), + emptyList(), + emptyList(), + null, + null, + emptyList(), + singletonList(infixReceiver) + ); + implicitParam = implicitParam.withMarkers(implicitParam.getMarkers().addIfAbsent(new TypeReferencePrefix(randomId(), Space.EMPTY))); + + List> newStatements = new ArrayList<>(params.getElements().size() + 1); + newStatements.add(JRightPadded.build(implicitParam)); + newStatements.addAll(params.getPadding().getElements()); + params = params.getPadding().withElements(newStatements); + + } + if (function.getTypeReference() != null) { markers = markers.addIfAbsent(new TypeReferencePrefix(randomId(), prefix(function.getColon()))); returnTypeExpression = function.getTypeReference().accept(this, data).withPrefix(prefix(function.getTypeReference())); diff --git a/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java b/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java index b613991a1..88035431c 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/ImportTest.java @@ -67,6 +67,13 @@ class A { ); } + @Test + void debug() { + rewriteRun( + kotlin("fun < T : Any > Class < T > . createInstance( ) { }") + ); + } + @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/158") @Test void methodName() {