Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the format of the data held in kotlin.MetaData documented anywhere?

Tags:

kotlin

I'm interested to know what data is held in the MetaData annotation added to each Kotlin class.

But most fields give no more detail than

"Metadata in a custom format. The format may be different (or even absent) for different kinds."

https://github.com/JetBrains/kotlin/blob/master/core/runtime.jvm/src/kotlin/Metadata.kt

Is there are reference somewhere that explains how to interpret this data?

like image 270
henry Avatar asked Apr 23 '16 21:04

henry


People also ask

What is metadata in Kotlin?

Metadata annotation is extra information stored in annotations in Java class files produced by the Kotlin JVM compiler. You will see the metadata annotation if you decompiled the java version of the kotlin class source code.

What is reflection Kotlin?

In Kotlin, Reflection is a combination of language and library capabilities that allow you to introspect a program while it's running. Kotlin reflection is used at runtime to utilize a class and its members, such as properties, methods, and constructors.


1 Answers

kotlin.Metadata contains information about Kotlin symbols, such as their names, signatures, relations between types, etc. Some of this information is already present in the JVM signatures in the class files, but a lot is not, since there's quite a few Kotlin-specific things which JVM class files cannot represent properly: type nullability, mutable/read-only collection interfaces, declaration-site variance, and others.

No specific actions were taken to make the schema of the data encoded in this annotation public, because for most users such data is needed to introspect a program at runtime, and the Kotlin reflection library provides a nice API for that.

If you need to inspect Kotlin-specific stuff which is not exposed via the reflection API, or you're just generally curious what else is stored in that annotation, you can take a look at the implementation of kotlinx.reflect.lite. It's a light-weight library, the core of which is the protobuf-generated schema parser. There's not much supported there at the moment, but there are schemas available which you can use to read any other data you need.

UPD (August 2018): since this was answered, we've published a new (experimental and unstable) library, which is designed to be the intended way for reading and modifying the metadata: https://discuss.kotlinlang.org/t/announcing-kotlinx-metadata-jvm-library-for-reading-modifying-metadata-of-kotlin-jvm-class-files/7980

like image 187
Alexander Udalov Avatar answered Oct 26 '22 17:10

Alexander Udalov