1
1
package io.bazel.kotlin.plugin.jdeps.k2
2
2
3
3
import org.jetbrains.kotlin.descriptors.SourceElement
4
- import org.jetbrains.kotlin.fir.java.JavaBinarySourceElement
5
4
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
6
5
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
7
6
import org.jetbrains.kotlin.name.ClassId
8
7
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
9
8
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
+
10
24
/* *
11
25
* Returns whether class is coming from JVM runtime env. There is no need to track these classes.
12
26
*
@@ -25,11 +39,16 @@ internal fun DeserializedContainerSource.classId(): ClassId? =
25
39
}
26
40
27
41
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
33
52
}
34
53
35
54
internal fun DeserializedContainerSource.binaryClass (): String? =
0 commit comments