Skip to content

Instantly share code, notes, and snippets.

@udalov
Last active March 1, 2018 17:03
Show Gist options
  • Save udalov/60c4cc51aa06278099b3f07e0224e7b8 to your computer and use it in GitHub Desktop.
Save udalov/60c4cc51aa06278099b3f07e0224e7b8 to your computer and use it in GitHub Desktop.
Load name of the original anonymous/synthetic class which was copied during inline (KT-21320)
import org.jetbrains.kotlin.serialization.jvm.*
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
fun loadOriginName(className: String) {
val metadata = Class.forName(className).declaredAnnotations
.filter { it.annotationClass.qualifiedName == "kotlin.Metadata" }
.single() as Metadata
if (metadata.k == 3 && metadata.d1.isNotEmpty()) {
// Synthetic classes generated for lambdas/coroutines
val (nameResolver, functionProto) = JvmProtoBufUtil.readFunctionDataFrom(metadata.d1, metadata.d2)
assert(functionProto.hasExtension(JvmProtoBuf.lambdaClassOriginName))
val nameIndex = functionProto.getExtension(JvmProtoBuf.lambdaClassOriginName)
val originName = nameResolver.getString(nameIndex)
println("Origin class for lambda class $className is $originName")
} else if (metadata.k == 1) {
// Anonymous classes generated for object literals
val (nameResolver, classProto) = JvmProtoBufUtil.readClassDataFrom(metadata.d1, metadata.d2)
assert(classProto.hasExtension(JvmProtoBuf.anonymousObjectOriginName))
val nameIndex = classProto.getExtension(JvmProtoBuf.anonymousObjectOriginName)
val originName = nameResolver.getString(nameIndex)
println("Origin class for anonymous object $className is $originName")
} else {
println("Unable to determine origin class for class $className")
}
}
fun main(args: Array<String>) {
// Example usage (replace with an existing class name):
loadOriginName("test.TestKt\$box\$\$inlined\$checker\$1")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment