Skip to content

Commit 83b9fd7

Browse files
committed
Add declaring type information for Kotlin top level functions
1 parent f8ade2a commit 83b9fd7

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

rewrite-kotlin/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeMapping.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,10 @@ class KotlinTypeMapping(
682682
return null
683683
}
684684
val signature = signatureBuilder.methodCallSignature(fir)
685-
val existing = typeCache.get<Method>(signature)
685+
/*val existing = typeCache.get<Method>(signature)
686686
if (existing != null) {
687687
return existing
688-
}
688+
}*/
689689
return methodInvocationType(fir, signature)
690690
}
691691

@@ -777,6 +777,11 @@ class KotlinTypeMapping(
777777
is Parameterized -> type.type
778778
else -> Unknown.getInstance()
779779
}
780+
} else if (resolvedSymbol.callableId.classId == null) {
781+
methodDeclarationType(resolvedSymbol.fir, null)
782+
declaringType = ShallowClass.build(resolvedSymbol.callableId.packageName.toString() + "." + firFile.name)
783+
//.withMethods(mutableListOf(methodDeclarationType(resolvedSymbol.fir, null)))
784+
//TODO: <need to add methods information here too
780785
}
781786
} else {
782787
declaringType = TypeUtils.asFullyQualified(type(function.typeRef))

rewrite-kotlin/src/main/kotlin/org/openrewrite/kotlin/KotlinTypeSignatureBuilder.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ class KotlinTypeSignatureBuilder(private val firSession: FirSession, private val
381381
) {
382382
declaringSig = "kotlin.Library"
383383
}
384+
} else if (resolvedSymbol.callableId.classId == null) {
385+
declaringSig = resolvedSymbol.callableId.packageName.toString() + "." + firFile.name
384386
}
385387
} else if (sym is FirFunctionSymbol<*>) {
386388
declaringSig = signature(function.typeRef)

rewrite-kotlin/src/test/java/org/openrewrite/kotlin/tree/KTSTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,29 @@ void topLevelAssignmentExpression() {
4444
void topLevelFunctionCall() {
4545
rewriteRun(
4646
kotlinScript(
47-
"""
48-
println("foo")
4947
"""
48+
package org.example
49+
50+
fun one() = "one"
51+
fun two() = "two"
52+
"""
53+
),
54+
kotlinScript(
55+
"""
56+
import org.example.one
57+
one("foo")
58+
""",
59+
spec -> spec.afterRecipe(cu -> {
60+
assertThat(cu.getTypesInUse().getUsedMethods())
61+
.singleElement()
62+
.satisfies(m -> {
63+
assertThat(m.getDeclaringType())
64+
.satisfies(it -> {
65+
assertThat(it.getFullyQualifiedName()).isEqualTo("org.example.openRewriteFile1.kts");
66+
assertThat(it.getMethods()).hasSize(2);
67+
});
68+
});
69+
})
5070
)
5171
);
5272
}
@@ -70,7 +90,6 @@ void dslSample() {
7090
rewriteRun(
7191
spec -> spec.typeValidationOptions(TypeValidation.none()),
7292
kotlinScript(
73-
//language=none
7493
"""
7594
plugins {
7695
id("org.flywaydb.flyway") version "8.0.2"

0 commit comments

Comments
 (0)