1
1
package io.bazel.kotlin.plugin.jdeps.k2
2
2
3
+ import io.bazel.kotlin.plugin.jdeps.k2.RefCache.vbseClass
4
+ import io.bazel.kotlin.plugin.jdeps.k2.RefCache.vbseGetVirtualFileMethod
3
5
import org.jetbrains.kotlin.descriptors.SourceElement
4
6
import org.jetbrains.kotlin.load.kotlin.JvmPackagePartSource
5
7
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
6
8
import org.jetbrains.kotlin.name.ClassId
7
9
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
8
10
9
11
private object RefCache {
12
+ val vbseClass: Class <* >? by lazy {
13
+ runCatching {
14
+ Class .forName(" org.jetbrains.kotlin.fir.java.VirtualFileBasedSourceElement" )
15
+ }.getOrNull()
16
+ }
17
+
18
+ val vbseGetVirtualFileMethod by lazy {
19
+ vbseClass
20
+ ?.runCatching {
21
+ getMethod(" getVirtualFile" )
22
+ }?.getOrNull()
23
+ }
24
+
10
25
val jbseClass: Class <* >? by lazy {
11
26
runCatching {
12
27
Class .forName(" org.jetbrains.kotlin.fir.java.JavaBinarySourceElement" )
@@ -28,8 +43,7 @@ private object RefCache {
28
43
* @return whether class is provided by JSM runtime or not
29
44
*/
30
45
internal fun isJvmClass (className : String ): Boolean =
31
- className.startsWith(" java" ) ||
32
- className.startsWith(" modules/java.base/java/" )
46
+ className.startsWith(" java" ) || className.startsWith(" modules/java.base/java/" )
33
47
34
48
internal fun DeserializedContainerSource.classId (): ClassId ? =
35
49
when (this ) {
@@ -43,6 +57,9 @@ internal fun SourceElement.binaryClass(): String? =
43
57
binaryClass.location
44
58
} else if (this is JvmPackagePartSource ) {
45
59
this .knownJvmBinaryClass?.location
60
+ } else if (vbseClass != null && vbseClass!! .isInstance(this )) {
61
+ val virtualFile = vbseGetVirtualFileMethod!! .invoke(this )
62
+ virtualFile?.javaClass!! .getMethod(" getPath" ).invoke(virtualFile) as ? String
46
63
} else if (RefCache .jbseClass != null && RefCache .jbseClass!! .isInstance(this )) {
47
64
val jClass = RefCache .jbseGetJavaClassMethod!! .invoke(this )
48
65
val virtualFile = jClass!! .javaClass.getMethod(" getVirtualFile" ).invoke(jClass)
0 commit comments