diff --git a/docs/changelog.md b/docs/changelog.md index 0c82d19d88..46b490ba1f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,7 @@ Change Log * Fix: Fix extension function imports (#1814). * Fix: Omit implicit modifiers on FileSpec.scriptBuilder (#1813). * Fix: Fix trailing newline in PropertySpec (#1827). +Change: kotlinx-metadata 0.9.0. Note that the `KotlinClassMetadata .read` is deprecated in 0.9.0 and replaced with `readStrict` (#1830). ## Version 1.16.0 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 85c61405d4..fc72c6803b 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -39,7 +39,7 @@ kotlin-compilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-emb kotlin-annotationProcessingEmbeddable = { module = "org.jetbrains.kotlin:kotlin-annotation-processing-embeddable", version.ref = "kotlin" } kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" } kotlin-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" } -kotlin-metadata = { module = "org.jetbrains.kotlinx:kotlinx-metadata-jvm", version = "0.8.0" } +kotlin-metadata = { module = "org.jetbrains.kotlinx:kotlinx-metadata-jvm", version = "0.9.0" } ksp = { module = "com.google.devtools.ksp:symbol-processing", version.ref = "ksp" } ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } diff --git a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/KotlinPoetMetadata.kt b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/KotlinPoetMetadata.kt index bae2474777..08338983cc 100644 --- a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/KotlinPoetMetadata.kt +++ b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/KotlinPoetMetadata.kt @@ -85,7 +85,7 @@ public inline fun Metadata.toKotlinClassMetada */ @KotlinPoetMetadataPreview public fun Metadata.readKotlinClassMetadata(): KotlinClassMetadata { - val metadata = KotlinClassMetadata.read(asClassHeader()) + val metadata = KotlinClassMetadata.readStrict(asClassHeader()) checkNotNull(metadata) { "Could not parse metadata! Try bumping kotlinpoet and/or kotlinx-metadata version." } diff --git a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt index 88c7b25bb1..7317a74050 100644 --- a/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt +++ b/interop/kotlinx-metadata/src/main/kotlin/com/squareup/kotlinpoet/metadata/specs/KotlinPoetMetadataSpecs.kt @@ -93,8 +93,6 @@ import kotlinx.metadata.Modality import kotlinx.metadata.Visibility import kotlinx.metadata.declaresDefaultValue import kotlinx.metadata.hasAnnotations -import kotlinx.metadata.hasGetter -import kotlinx.metadata.hasSetter import kotlinx.metadata.isConst import kotlinx.metadata.isCrossinline import kotlinx.metadata.isData @@ -648,49 +646,45 @@ private fun KmProperty.toPropertySpec( val returnTypeName = returnType.toTypeName(typeParamResolver) val mutableAnnotations = mutableListOf() if (containerData != null && propertyData != null) { - if (hasGetter) { - getterSignature?.let { getterSignature -> - if (!containerData.isInterface && - modality != Modality.OPEN && modality != Modality.ABSTRACT + getterSignature?.let { getterSignature -> + if (!containerData.isInterface && + modality != Modality.OPEN && modality != Modality.ABSTRACT + ) { + // Infer if JvmName was used + // We skip interface types or open/abstract properties because they can't have @JvmName. + // For annotation properties, kotlinc puts JvmName annotations by default in + // bytecode but they're implicit in source, so we expect the simple name for + // annotation types. + val expectedMetadataName = if (containerData is ClassData && + containerData.declarationContainer.isAnnotation ) { - // Infer if JvmName was used - // We skip interface types or open/abstract properties because they can't have @JvmName. - // For annotation properties, kotlinc puts JvmName annotations by default in - // bytecode but they're implicit in source, so we expect the simple name for - // annotation types. - val expectedMetadataName = if (containerData is ClassData && - containerData.declarationContainer.isAnnotation - ) { - name - } else { - "get${name.safeCapitalize(Locale.US)}" - } - getterSignature.jvmNameAnnotation( - metadataName = expectedMetadataName, - useSiteTarget = UseSiteTarget.GET, - )?.let { jvmNameAnnotation -> - mutableAnnotations += jvmNameAnnotation - } + name + } else { + "get${name.safeCapitalize(Locale.US)}" + } + getterSignature.jvmNameAnnotation( + metadataName = expectedMetadataName, + useSiteTarget = UseSiteTarget.GET, + )?.let { jvmNameAnnotation -> + mutableAnnotations += jvmNameAnnotation } } } - if (hasSetter) { - setterSignature?.let { setterSignature -> - if (containerData is ClassData && - !containerData.declarationContainer.isAnnotation && - !containerData.declarationContainer.isInterface && - classInspector?.supportsNonRuntimeRetainedAnnotations == false && - modality != Modality.OPEN && modality != Modality.ABSTRACT - ) { - // Infer if JvmName was used - // We skip annotation types for this because they can't have vars. - // We skip interface types or open/abstract properties because they can't have @JvmName. - setterSignature.jvmNameAnnotation( - metadataName = "set${name.safeCapitalize(Locale.US)}", - useSiteTarget = UseSiteTarget.SET, - )?.let { jvmNameAnnotation -> - mutableAnnotations += jvmNameAnnotation - } + setterSignature?.let { setterSignature -> + if (containerData is ClassData && + !containerData.declarationContainer.isAnnotation && + !containerData.declarationContainer.isInterface && + classInspector?.supportsNonRuntimeRetainedAnnotations == false && + modality != Modality.OPEN && modality != Modality.ABSTRACT + ) { + // Infer if JvmName was used + // We skip annotation types for this because they can't have vars. + // We skip interface types or open/abstract properties because they can't have @JvmName. + setterSignature.jvmNameAnnotation( + metadataName = "set${name.safeCapitalize(Locale.US)}", + useSiteTarget = UseSiteTarget.SET, + )?.let { jvmNameAnnotation -> + mutableAnnotations += jvmNameAnnotation } } } @@ -777,16 +771,16 @@ private fun KmProperty.toPropertySpec( // since the delegate handles it // vals with initialized constants have a getter in bytecode but not a body in kotlin source val modifierSet = modifiers.toSet() - if (hasGetter && !isDelegated && modality != Modality.ABSTRACT) { + if (!isDelegated && modality != Modality.ABSTRACT) { propertyAccessor( modifierSet, - getter, + this@toPropertySpec.getter, FunSpec.getterBuilder().addStatement(NOT_IMPLEMENTED), isOverride, )?.let(::getter) } - if (hasSetter && !isDelegated && modality != Modality.ABSTRACT) { - propertyAccessor(modifierSet, setter!!, FunSpec.setterBuilder(), isOverride)?.let(::setter) + if (setter != null && !isDelegated && modality != Modality.ABSTRACT) { + propertyAccessor(modifierSet, this@toPropertySpec.setter!!, FunSpec.setterBuilder(), isOverride)?.let(::setter) } } .tag(this)