Skip to content

Commit

Permalink
support implicit type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Oct 10, 2023
1 parent 4ed38f6 commit 37dde26
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<KtTypeParameter> ktTypeParameters = function.getTypeParameters();
List<JRightPadded<J.TypeParameter>> 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<KtTypeParameter> typeParams = function.getTypeParameters();

boolean isOpen = function.hasModifier(KtTokens.OPEN_KEYWORD);
Expand Down Expand Up @@ -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<J.VariableDeclarations.NamedVariable> 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(),
"<receiverType>",
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<JRightPadded<Statement>> 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()));
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/openrewrite/kotlin/tree/ImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 37dde26

Please sign in to comment.