-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kotlin] Add capability to fetch @kotlin.Metadata in the debugger
- Loading branch information
1 parent
3106110
commit 1dc6149
Showing
3 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
plugins/kotlin/jvm-debugger/core/src/org/jetbrains/kotlin/idea/debugger/core/metadataUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
|
||
package org.jetbrains.kotlin.idea.debugger.core | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.JsonSyntaxException | ||
import com.intellij.debugger.engine.evaluation.EvaluationContext | ||
import com.sun.jdi.* | ||
import kotlinx.metadata.jvm.KotlinClassMetadata | ||
import org.jetbrains.kotlin.idea.debugger.base.util.wrapEvaluateException | ||
import org.jetbrains.kotlin.idea.debugger.base.util.wrapIllegalArgumentException | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
|
||
fun ReferenceType.fetchKotlinMetadata(context: EvaluationContext): KotlinClassMetadata? { | ||
val classObject = classObject() ?: return null | ||
|
||
val debugProcess = context.debugProcess | ||
val metadataUtilClass = wrapEvaluateException { | ||
debugProcess.findClass(context, METADATA_UTILS_CLASS_NAME, null) | ||
} as? ClassType ?: return null | ||
val getDebugMetadataAsJsonMethod = metadataUtilClass.methodsByName(GET_DEBUG_METADATA_AS_JSON).singleOrNull() | ||
?: return null | ||
val metadataAsJson = wrapEvaluateException { | ||
debugProcess.invokeMethod(context, metadataUtilClass, getDebugMetadataAsJsonMethod, listOf(classObject)) | ||
} as? StringReference ?: return null | ||
|
||
val metadata = wrapJsonSyntaxException { | ||
Gson().fromJson(metadataAsJson.value(), MetadataAdapter::class.java).toMetadata() | ||
} ?: return null | ||
|
||
return wrapIllegalArgumentException { | ||
KotlinClassMetadata.readStrict(metadata) | ||
} | ||
} | ||
|
||
private const val METADATA_UTILS_CLASS_NAME = "kotlin.jvm.internal.MetadataDebugUtilKt" | ||
private const val GET_DEBUG_METADATA_AS_JSON = "getDebugMetadataAsJson" | ||
|
||
private class MetadataAdapter( | ||
val kind: Int, | ||
val metadataVersion: Array<Int>, | ||
val data1: Array<String>, | ||
val data2: Array<String>, | ||
val extraString: String, | ||
val packageName: String, | ||
val extraInt: Int, | ||
) { | ||
@OptIn(ExperimentalEncodingApi::class) | ||
fun toMetadata(): Metadata { | ||
return Metadata( | ||
kind = kind, | ||
metadataVersion = metadataVersion.toIntArray(), | ||
data1 = data1.map { String(Base64.Default.decode(it)) }.toTypedArray(), | ||
data2 = data2, | ||
extraString = extraString, | ||
packageName = packageName, | ||
extraInt = extraInt | ||
) | ||
} | ||
} | ||
|
||
private fun <T> wrapJsonSyntaxException(block: () -> T): T? { | ||
return try { | ||
block() | ||
} catch (e: JsonSyntaxException) { | ||
null | ||
} | ||
} |