KotlinClassMetadata

sealed class KotlinClassMetadata

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.readStrict or KotlinClassMetadata.readLenient, depending on your application (see 'Working with different versions' section in the readme).

Metadata annotation can be obtained either via reflection or created from data from a binary class file, using its constructor or helper function kotlin.metadata.jvm.Metadata.

KotlinClassMetadata alone exposes only KotlinClassMetadata.version and KotlinClassMetadata.flags; 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 to a Km data structure or to KotlinClassMetadata itself, it is possible to get a new raw metadata instance with a write function.

If metadata has been read with readLenient, write will throw an exception.

Here is an example of reading a content name of some metadata:

fun displayName(metadata: Metadata): String = when (val kcm = KotlinClassMetadata.readStrict(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"
}

In this example, only introspection (reading) is performed. In such cases, to support broader range of metadata versions, you may also use readLenient method.

Inheritors

Types

Link copied to clipboard
class Class(var kmClass: KmClass, var version: JvmMetadataVersion, var flags: Int) : KotlinClassMetadata

Represents metadata of a class file containing a declaration of a Kotlin class.

Link copied to clipboard
object Companion

Collection of methods for reading and writing KotlinClassMetadata, as well as metadata kind constants.

Link copied to clipboard
class FileFacade(var kmPackage: KmPackage, var version: JvmMetadataVersion, var flags: Int) : KotlinClassMetadata

Represents metadata of a class file containing a compiled Kotlin file facade.

Link copied to clipboard
class MultiFileClassFacade(var partClassNames: List<String>, var version: JvmMetadataVersion, var flags: Int) : KotlinClassMetadata

Represents metadata of a class file containing a compiled multi-file class facade.

Link copied to clipboard
class MultiFileClassPart(var kmPackage: KmPackage, var facadeClassName: String, var version: JvmMetadataVersion, var flags: Int) : KotlinClassMetadata

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.

Link copied to clipboard
class SyntheticClass(var kmLambda: KmLambda?, var version: JvmMetadataVersion, var flags: Int) : KotlinClassMetadata

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.

Link copied to clipboard

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.

Properties

Link copied to clipboard
abstract var flags: Int

Additional classfile-level flags of this metadata. See Metadata.extraInt for possible values.

Link copied to clipboard

Version of this metadata.

Functions

Link copied to clipboard
abstract fun write(): Metadata

Encodes and writes this metadata to the new instance of Metadata.