Why is Kotlin keeping MetaData in Kotlin class file?
We converted the Java file to a Kotlin file and then compiled it into a class file. The resulting class is larger than the original Java class file. In the Kotlin class file, we found metadata in each class. Why does Kotlin store this metadata?
Certain aspects of Kotlin code cannot be expressed in pure Java bytecode (e.g. nullability, primary constructors, internal
visibility, modifier lateinit
, property delegation, etc.). Instead, they are serialized and stored as metadata in the classes.
When you use a JAR distribution of a Kotlin library, the compiler parses the metadata to discover these specific Kotlin attributes in order to use the library as a Kotlin artifact with all the available functionality.
At runtime, the metadata is used by the kotlin-reflect
Kotlin reflection API to restore these attributes and make them available through reflection.