A Kotlin library for reading and modifying binary metadata in Kotlin/JVM compiler-generated files.
—
Version handling system for ensuring compatibility between different Kotlin compiler versions and metadata formats. The version management system ensures that metadata can be read and written correctly across different Kotlin compiler versions.
Represents a version for JVM metadata with major, minor, and patch components.
/**
* Represents a version for JVM metadata
* @param major Major version number
* @param minor Minor version number
* @param patch Patch version number (defaults to 0)
*/
class JvmMetadataVersion(
val major: Int,
val minor: Int,
val patch: Int = 0
) : Comparable<JvmMetadataVersion> {
companion object {
/** Latest stable supported metadata version */
val LATEST_STABLE_SUPPORTED: JvmMetadataVersion
/** Highest metadata version allowed for writing */
val HIGHEST_ALLOWED_TO_WRITE: JvmMetadataVersion
}
}Multiple constructor options for creating version instances.
/**
* Primary constructor with all version components
* @param major Major version number
* @param minor Minor version number
* @param patch Patch version number
*/
constructor(major: Int, minor: Int, patch: Int)
/**
* Secondary constructor with patch defaulting to 0
* @param major Major version number
* @param minor Minor version number
*/
constructor(major: Int, minor: Int)Usage Examples:
import kotlin.metadata.jvm.JvmMetadataVersion
// Create version with all components
val version1 = JvmMetadataVersion(1, 6, 0)
// Create version with patch defaulting to 0
val version2 = JvmMetadataVersion(1, 7)
// Access version properties
println("Major: ${version1.major}")
println("Minor: ${version1.minor}")
println("Patch: ${version1.patch}")
println("Version string: ${version1}")Version comparison and equality operations.
/**
* Compares this version with another version
* @param other The version to compare against
* @return Negative value if this < other, 0 if equal, positive if this > other
*/
override fun compareTo(other: JvmMetadataVersion): Int
/**
* Returns string representation of the version
* @return String in format "major.minor.patch"
*/
override fun toString(): String
/**
* Calculates hash code for the version
* @return Hash code based on major, minor, and patch values
*/
override fun hashCode(): Int
/**
* Checks equality with another object
* @param other Object to compare against
* @return true if other is JvmMetadataVersion with same major, minor, patch
*/
override fun equals(other: Any?): BooleanUsage Examples:
val version1 = JvmMetadataVersion(1, 6, 0)
val version2 = JvmMetadataVersion(1, 7, 0)
val version3 = JvmMetadataVersion(1, 6, 0)
// Version comparison
println(version1 < version2) // true
println(version1 > version2) // false
println(version1 == version3) // true
// Sorting versions
val versions = listOf(
JvmMetadataVersion(1, 8, 0),
JvmMetadataVersion(1, 6, 0),
JvmMetadataVersion(1, 7, 0)
)
val sortedVersions = versions.sorted()
println(sortedVersions) // [1.6.0, 1.7.0, 1.8.0]
// String representation
println(version1.toString()) // "1.6.0"Pre-defined version constants for common use cases.
companion object {
/**
* Latest stable supported version for reading metadata
* This represents the highest version that can be reliably read
*/
val LATEST_STABLE_SUPPORTED: JvmMetadataVersion
/**
* Highest version allowed for writing metadata
* Writing metadata with versions higher than this may cause compatibility issues
*/
val HIGHEST_ALLOWED_TO_WRITE: JvmMetadataVersion
}Usage Examples:
// Check version compatibility
val currentVersion = JvmMetadataVersion(1, 6, 0)
if (currentVersion <= JvmMetadataVersion.LATEST_STABLE_SUPPORTED) {
println("Version is supported for reading")
}
if (currentVersion <= JvmMetadataVersion.HIGHEST_ALLOWED_TO_WRITE) {
println("Version is safe for writing")
}
// Use in metadata operations
val moduleMetadata = KotlinModuleMetadata(
kmModule = module,
version = JvmMetadataVersion.LATEST_STABLE_SUPPORTED
)Understanding and handling version compatibility in metadata operations.
// Version compatibility checking
fun isCompatibleForReading(version: JvmMetadataVersion): Boolean {
return version <= JvmMetadataVersion.LATEST_STABLE_SUPPORTED
}
fun isCompatibleForWriting(version: JvmMetadataVersion): Boolean {
return version <= JvmMetadataVersion.HIGHEST_ALLOWED_TO_WRITE
}
// Example usage in metadata reading
val metadataVersion = JvmMetadataVersion(1, 8, 0)
val metadata = if (isCompatibleForReading(metadataVersion)) {
KotlinClassMetadata.readStrict(annotationData)
} else {
// Fall back to lenient reading for newer versions
KotlinClassMetadata.readLenient(annotationData)
}Practical examples of version handling in metadata operations.
// Extract version from existing metadata
when (val metadata = KotlinClassMetadata.readLenient(annotationData)) {
is KotlinClassMetadata.Class -> {
val version = metadata.version
println("Class metadata version: $version")
// Check if version supports certain features
if (version >= JvmMetadataVersion(1, 6)) {
// Version 1.6+ features available
}
}
}
// Create metadata with specific version
val targetVersion = JvmMetadataVersion(1, 6, 0)
val moduleMetadata = KotlinModuleMetadata(
kmModule = createModule(),
version = targetVersion
)
// Version-aware metadata transformation
KotlinClassMetadata.transform(originalMetadata) { metadata ->
// Ensure we don't exceed supported versions
if (metadata.version <= JvmMetadataVersion.HIGHEST_ALLOWED_TO_WRITE) {
// Safe to modify
when (metadata) {
is KotlinClassMetadata.Class -> {
// Modify class metadata
}
}
}
}Kotlin metadata versions typically follow the Kotlin compiler version scheme:
Understanding version compatibility is crucial for:
Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-metadata-jvm