From fc1afa91db5c68754da0f74e42ea981e0f6f1587 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Sun, 8 Oct 2023 09:06:02 -0700 Subject: [PATCH] support wildcard type --- .../internal/KotlinTreeParserVisitor.java | 34 ++++++++++++++++--- .../kotlin/tree/VariableDeclarationTest.java | 2 +- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java index 37914654d..d8b5b774b 100644 --- a/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java +++ b/src/main/java/org/openrewrite/kotlin/internal/KotlinTreeParserVisitor.java @@ -724,7 +724,15 @@ public J visitTypeParameterList(KtTypeParameterList list, ExecutionContext data) @Override public J visitTypeProjection(KtTypeProjection typeProjection, ExecutionContext data) { - return typeProjection.getTypeReference().accept(this, data); + if (typeProjection.getTypeReference() != null) { + return typeProjection.getTypeReference().accept(this, data); + } + + if (typeProjection.getProjectionKind() == KtProjectionKind.STAR) { + return new J.Wildcard(randomId(), prefix(typeProjection), Markers.EMPTY, null, null); + } + + throw new UnsupportedOperationException("TODO"); } @Override @@ -1872,11 +1880,29 @@ public J visitTypeReference(KtTypeReference typeReference, ExecutionContext data @Override public J visitUserType(KtUserType type, ExecutionContext data) { - if (!type.getTypeArguments().isEmpty()) { - throw new UnsupportedOperationException("TODO"); + + NameTree nameTree = type.getReferenceExpression().accept(this, data).withPrefix(prefix(type)); + + List typeArguments = type.getTypeArguments(); + List> parameters = new ArrayList<>(typeArguments.size()); + if (!typeArguments.isEmpty()) { + for (KtTypeProjection typeProjection : typeArguments) { + parameters.add(padRight(convertToExpression(typeProjection.accept(this, data)), suffix(typeProjection))); + } + + return new J.ParameterizedType( + randomId(), + Space.EMPTY, + Markers.EMPTY, + nameTree, + JContainer.build(prefix(type.getTypeArgumentList()), parameters, Markers.EMPTY), + type(type) + ); } + + // TODO: fix NPE. - return type.getReferenceExpression().accept(this, data); + return nameTree; } @Override diff --git a/src/test/java/org/openrewrite/kotlin/tree/VariableDeclarationTest.java b/src/test/java/org/openrewrite/kotlin/tree/VariableDeclarationTest.java index 939f533b3..e19db6472 100644 --- a/src/test/java/org/openrewrite/kotlin/tree/VariableDeclarationTest.java +++ b/src/test/java/org/openrewrite/kotlin/tree/VariableDeclarationTest.java @@ -252,7 +252,7 @@ class Test < T > kotlin( """ import org.foo.Test - val a : Test < * > = null + val a : Test < * > = null """ ) );