Skip to content

Commit 9a0fe8d

Browse files
authored
Kotlin 2.1.10+ prep (#1299)
* Kotlin 2.1.10+ prep * More * Formatting
1 parent c1f6d80 commit 9a0fe8d

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/main/kotlin/io/bazel/kotlin/plugin/jdeps/k2/JdepsK2Utils.kt

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
package io.bazel.kotlin.plugin.jdeps.k2
22

33
import org.jetbrains.kotlin.descriptors.SourceElement
4-
import org.jetbrains.kotlin.fir.java.JavaBinarySourceElement
54
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
65
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
76
import org.jetbrains.kotlin.name.ClassId
87
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
98

9+
private object RefCache {
10+
val jbseClass: Class<*>? by lazy {
11+
runCatching {
12+
Class.forName("org.jetbrains.kotlin.fir.java.JavaBinarySourceElement")
13+
}.getOrNull()
14+
}
15+
16+
val jbseGetJavaClassMethod by lazy {
17+
jbseClass
18+
?.runCatching {
19+
getMethod("getJavaClass")
20+
}?.getOrNull()
21+
}
22+
}
23+
1024
/**
1125
* Returns whether class is coming from JVM runtime env. There is no need to track these classes.
1226
*
@@ -25,11 +39,16 @@ internal fun DeserializedContainerSource.classId(): ClassId? =
2539
}
2640

2741
internal fun SourceElement.binaryClass(): String? =
28-
when (this) {
29-
is KotlinJvmBinarySourceElement -> binaryClass.location
30-
is JvmPackagePartSource -> this.knownJvmBinaryClass?.location
31-
is JavaBinarySourceElement -> this.javaClass.virtualFile.path
32-
else -> null
42+
if (this is KotlinJvmBinarySourceElement) {
43+
binaryClass.location
44+
} else if (this is JvmPackagePartSource) {
45+
this.knownJvmBinaryClass?.location
46+
} else if (RefCache.jbseClass != null && RefCache.jbseClass!!.isInstance(this)) {
47+
val jClass = RefCache.jbseGetJavaClassMethod!!.invoke(this)
48+
val virtualFile = jClass!!.javaClass.getMethod("getVirtualFile").invoke(jClass)
49+
virtualFile.javaClass.getMethod("getPath").invoke(virtualFile) as? String
50+
} else {
51+
null
3352
}
3453

3554
internal fun DeserializedContainerSource.binaryClass(): String? =

0 commit comments

Comments
 (0)