Skip to content

Commit 8fed615

Browse files
committed
Add methods
1 parent 1636d04 commit 8fed615

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ import org.jetbrains.kotlin.fir.references.FirResolvedNamedReference
3939
import org.jetbrains.kotlin.fir.references.FirSuperReference
4040
import org.jetbrains.kotlin.fir.references.toResolvedBaseSymbol
4141
import org.jetbrains.kotlin.fir.resolve.calls.FirSyntheticFunctionSymbol
42+
import org.jetbrains.kotlin.fir.resolve.providers.firProvider
4243
import org.jetbrains.kotlin.fir.resolve.providers.toSymbol
4344
import org.jetbrains.kotlin.fir.resolve.toFirRegularClass
4445
import org.jetbrains.kotlin.fir.resolve.toSymbol
46+
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
4547
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
4648
import org.jetbrains.kotlin.fir.symbols.impl.*
4749
import org.jetbrains.kotlin.fir.types.*
@@ -59,11 +61,11 @@ import org.openrewrite.java.JavaTypeMapping
5961
import org.openrewrite.java.internal.JavaTypeCache
6062
import org.openrewrite.java.tree.JavaType
6163
import org.openrewrite.java.tree.JavaType.*
64+
import org.openrewrite.java.tree.JavaType.Array
6265
import org.openrewrite.java.tree.TypeUtils
6366
import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.convertClassIdToFqn
6467
import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.methodName
6568
import org.openrewrite.kotlin.KotlinTypeSignatureBuilder.Companion.variableName
66-
import kotlin.collections.ArrayList
6769

6870
@Suppress("DuplicatedCode")
6971
class KotlinTypeMapping(
@@ -778,10 +780,9 @@ class KotlinTypeMapping(
778780
else -> Unknown.getInstance()
779781
}
780782
} else if (resolvedSymbol.callableId.classId == null) {
781-
methodDeclarationType(resolvedSymbol.fir, null)
783+
val topLevelFunctions = resolvedSymbol.getContainingFile()?.collectTopLevelFunctions() ?: emptyList()
782784
declaringType = ShallowClass.build(resolvedSymbol.callableId.packageName.toString() + "." + firFile.name.replaceFirst(".kts", "Kt").replaceFirst(".kt", "Kt"))
783-
//.withMethods(mutableListOf(methodDeclarationType(resolvedSymbol.fir, null)))
784-
//TODO: <need to add methods information here too
785+
.withMethods(topLevelFunctions.map { methodDeclarationType(it, null) })
785786
}
786787
} else {
787788
declaringType = TypeUtils.asFullyQualified(type(function.typeRef))
@@ -1380,4 +1381,22 @@ class KotlinTypeMapping(
13801381
}
13811382
}
13821383
}
1384+
1385+
private fun FirBasedSymbol<*>.getContainingFile() =
1386+
when (this) {
1387+
is FirCallableSymbol<*> -> moduleData.session.firProvider.getFirCallableContainerFile(this)
1388+
is FirClassLikeSymbol<*> -> moduleData.session.firProvider.getFirClassifierContainerFileIfAny(this)
1389+
else -> null
1390+
}
1391+
1392+
fun FirFile.collectTopLevelFunctions(): List<FirSimpleFunction> =
1393+
buildList {
1394+
declarations.forEach {
1395+
when (it) {
1396+
is FirSimpleFunction -> add(it)
1397+
is FirScript -> it.statements.filterIsInstance<FirSimpleFunction>().forEach(::add)
1398+
else -> {}
1399+
}
1400+
}
1401+
}
13831402
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.java.tree.J;
20+
import org.openrewrite.java.tree.JavaType;
2021
import org.openrewrite.kotlin.KotlinIsoVisitor;
2122
import org.openrewrite.test.RewriteTest;
2223
import org.openrewrite.test.TypeValidation;
@@ -63,7 +64,7 @@ fun two() = "two"
6364
assertThat(m.getDeclaringType())
6465
.satisfies(it -> {
6566
assertThat(it.getFullyQualifiedName()).isEqualTo("org.example.openRewriteFile1Kt");
66-
assertThat(it.getMethods()).hasSize(2);
67+
assertThat(it.getMethods()).extracting(JavaType.Method::getName).containsExactlyInAnyOrder("one", "two");
6768
});
6869
})
6970
)

0 commit comments

Comments
 (0)