1
1
package io.bazel.kotlin.plugin.jdeps.k2
2
2
3
+ import io.bazel.kotlin.plugin.jdeps.k2.RefCache.jbseClass
4
+ import io.bazel.kotlin.plugin.jdeps.k2.RefCache.jbseGetJavaClassMethod
5
+ import io.bazel.kotlin.plugin.jdeps.k2.RefCache.vbseClass
6
+ import io.bazel.kotlin.plugin.jdeps.k2.RefCache.vbseGetVirtualFileMethod
3
7
import org.jetbrains.kotlin.descriptors.SourceElement
4
- import org.jetbrains.kotlin.fir.java.VirtualFileBasedSourceElement
5
8
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
6
9
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
7
10
import org.jetbrains.kotlin.name.ClassId
8
11
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
9
12
13
+ private object RefCache {
14
+
15
+ val vbseClass: Class <* >? by lazy {
16
+ runCatching {
17
+ Class .forName(" org.jetbrains.kotlin.fir.java.VirtualFileBasedSourceElement" )
18
+ }.getOrNull()
19
+ }
20
+
21
+ val vbseGetVirtualFileMethod by lazy {
22
+ vbseClass?.runCatching {
23
+ getMethod(" getVirtualFile" )
24
+ }?.getOrNull()
25
+ }
26
+
27
+ val jbseClass: Class <* >? by lazy {
28
+ runCatching {
29
+ Class .forName(" org.jetbrains.kotlin.fir.descriptors.impl.JavaBinarySourceElement" )
30
+ }.getOrNull()
31
+ }
32
+
33
+ val jbseGetJavaClassMethod by lazy {
34
+ jbseClass?.runCatching {
35
+ getMethod(" getJavaClass" )
36
+ }?.getOrNull()
37
+ }
38
+ }
39
+
40
+
10
41
/* *
11
42
* Returns whether class is coming from JVM runtime env. There is no need to track these classes.
12
43
*
@@ -25,11 +56,21 @@ internal fun DeserializedContainerSource.classId(): ClassId? =
25
56
}
26
57
27
58
internal fun SourceElement.binaryClass (): String? =
28
- when (this ) {
29
- is KotlinJvmBinarySourceElement -> binaryClass.location
30
- is JvmPackagePartSource -> this .knownJvmBinaryClass?.location
31
- is VirtualFileBasedSourceElement -> this .virtualFile.path
32
- else -> null
59
+ if (this is KotlinJvmBinarySourceElement ) {
60
+ binaryClass.location
61
+ } else if (this is JvmPackagePartSource ) {
62
+ this .knownJvmBinaryClass?.location
63
+ } else if (vbseClass != null && vbseClass!! .isInstance(this )) {
64
+ // 1. Try VirtualFileBasedSourceElement (Kotlin 2.1.20+)
65
+ val virtualFile = vbseGetVirtualFileMethod?.invoke(this )
66
+ virtualFile?.javaClass?.getMethod(" getPath" )?.invoke(virtualFile) as ? String
67
+ } else if (jbseClass != null && jbseClass!! .isInstance(this )) {
68
+ // 2. If that fails, try the old class name: JavaBinarySourceElement
69
+ val jClass = jbseGetJavaClassMethod?.invoke(this )
70
+ val virtualFile = jClass?.javaClass?.getMethod(" getVirtualFile" )?.invoke(jClass)
71
+ virtualFile?.javaClass?.getMethod(" getPath" )?.invoke(virtualFile) as ? String
72
+ } else {
73
+ null
33
74
}
34
75
35
76
internal fun DeserializedContainerSource.binaryClass (): String? =
0 commit comments