Kotlin Class Metadata
Represents the parsed metadata of a Kotlin JVM class file. Entry point for parsing metadata on JVM.
To create an instance of KotlinClassMetadata, first obtain an instance of Metadata annotation on a class file, and then call KotlinClassMetadata.read. Metadata annotation can be obtained either via reflection or created from data from a binary class file, using its constructor or helper function kotlinx.metadata.jvm.Metadata.
KotlinClassMetadata itself does not have any meaningful methods or properties; to work with it, it is required to do a when
over subclasses. Different subclasses of KotlinClassMetadata represent different kinds of class files produced by Kotlin compiler. It is recommended to study documentation for each subclass and decide what subclasses one has to handle in the when
expression, trying to cover as much as possible. Normally, one would need at least a Class and a FileFacade, as these are two most common kinds.
Most of the subclasses declare a property to view metadata as a Km data structure — for example, KotlinClassMetadata.Class.kmClass. Some of them also can contain additional properties, e.g. KotlinClassMetadata.MultiFileClassPart.facadeClassName. Km data structures represent Kotlin declarations and offer a variety of properties to introspect and alter them. After desired changes are made, it is possible to get a new raw metadata instance with a corresponding write
function, such as KotlinClassMetadata.writeClass.
Here is an example of reading a content name of some metadata:
fun displayName(metadata: Metadata): String = when (val kcm = KotlinClassMetadata.read(metadata)) {
is KotlinClassMetadata.Class -> "Class ${kcm.kmClass.name}"
is KotlinClassMetadata.FileFacade -> "File facade with functions: ${kcm.kmPackage.functions.joinToString { it.name }}"
is KotlinClassMetadata.SyntheticClass -> kcm.kmLambda?.function?.name?.let { "Lambda $it" } ?: "Synthetic class"
is KotlinClassMetadata.MultiFileClassFacade -> "Multifile class facade with parts: ${kcm.partClassNames.joinToString()}"
is KotlinClassMetadata.MultiFileClassPart -> "Multifile class part ${kcm.facadeClassName}"
is KotlinClassMetadata.Unknown -> "Unknown metadata"
}
Inheritors
Types
Represents metadata of a class file containing a declaration of a Kotlin class.
Collection of methods for reading and writing KotlinClassMetadata, as well as metadata kind constants and COMPATIBLE_METADATA_VERSION constant.
Represents metadata of a class file containing a compiled Kotlin file facade.
Represents metadata of a class file containing a compiled multi-file class facade.
Represents metadata of a class file containing a compiled multi-file class part, i.e. an internal class with method bodies and their metadata, accessed only from the corresponding facade. Just like FileFacade, this metadata contains only top-level declarations, as classes have their own one.
Represents metadata of a class file containing a synthetic class, e.g. a class for lambda, $DefaultImpls
class for interface method implementations, $WhenMappings
class for optimized when
over enums, etc.
Represents metadata of an unknown class file. This class is used if an old version of this library is used against a new kind of class files generated by the Kotlin compiler, unsupported by this library.