diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f66e32b..e092775 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,7 +19,7 @@ incap = "0.3" jvmTarget = "1.8" kotlin = "1.5.21" kotlinCompileTesting = "1.4.3" -kotlinpoet = "1.9.0" +kotlinpoet = "1.10.0" ktlint = "0.41.0" [plugins] @@ -34,15 +34,14 @@ asm = "org.ow2.asm:asm:9.2" autoCommon = "com.google.auto:auto-common:1.1" autoService = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" } autoService-processor = { module = "com.google.auto.service:auto-service", version.ref = "autoService" } +guava = { module = "com.google.guava:guava", version = "30.1.1-jre" } incap = { module = "net.ltgt.gradle.incap:incap", version.ref = "incap" } incap-processor = { module = "net.ltgt.gradle.incap:incap-processor", version.ref = "incap" } jsr305 = "com.google.code.findbugs:jsr305:3.0.2" kotlin-compilerEmbeddable = { module = "org.jetbrains.kotlin:kotlin-compiler-embeddable", version.ref = "kotlin" } kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" } kotlinpoet = { module = "com.squareup:kotlinpoet", version.ref = "kotlinpoet" } -kotlinpoet-elementsClassInspector = { module = "com.squareup:kotlinpoet-classinspector-elements", version.ref = "kotlinpoet" } -kotlinpoet-metadata-core = { module = "com.squareup:kotlinpoet-metadata", version.ref = "kotlinpoet" } -kotlinpoet-metadata-specs = { module = "com.squareup:kotlinpoet-metadata-specs", version.ref = "kotlinpoet" } +kotlinpoet-metadata = { module = "com.squareup:kotlinpoet-metadata", version.ref = "kotlinpoet" } kotlinxMetadata = "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.3.0" okio = "com.squareup.okio:okio:2.10.0" diff --git a/kotlin/codegen/build.gradle.kts b/kotlin/codegen/build.gradle.kts index 4a9389f..67a3544 100644 --- a/kotlin/codegen/build.gradle.kts +++ b/kotlin/codegen/build.gradle.kts @@ -62,20 +62,12 @@ dependencies { exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib") } api(libs.kotlinpoet) - shade(libs.kotlinpoet.metadata.core) { - exclude(group = "org.jetbrains.kotlin") - exclude(group = "com.squareup", module = "kotlinpoet") - } - shade(libs.kotlinpoet.metadata.specs) { - exclude(group = "org.jetbrains.kotlin") - exclude(group = "com.squareup", module = "kotlinpoet") - } - api(libs.kotlinpoet.elementsClassInspector) - shade(libs.kotlinpoet.elementsClassInspector) { + shade(libs.kotlinpoet.metadata) { exclude(group = "org.jetbrains.kotlin") exclude(group = "com.squareup", module = "kotlinpoet") exclude(group = "com.google.guava") } + api(libs.guava) api(libs.asm) api(libs.autoService) @@ -84,9 +76,7 @@ dependencies { kapt(libs.incap.processor) // Copy these again as they're not automatically included since they're shaded - testImplementation(libs.kotlinpoet.metadata.core) - testImplementation(libs.kotlinpoet.metadata.specs) - testImplementation(libs.kotlinpoet.elementsClassInspector) + testImplementation(libs.kotlinpoet.metadata) testImplementation(libs.junit) testImplementation(libs.truth) testImplementation(libs.kotlinCompileTesting) diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/JsonClassCodegenProcessor.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/JsonClassCodegenProcessor.kt index e2bb823..7ed9beb 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/JsonClassCodegenProcessor.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/JsonClassCodegenProcessor.kt @@ -18,7 +18,7 @@ package com.squareup.moshi.kotlin.codegen.apt import com.google.auto.service.AutoService import com.squareup.kotlinpoet.AnnotationSpec import com.squareup.kotlinpoet.ClassName -import com.squareup.kotlinpoet.classinspector.elements.ElementsClassInspector +import com.squareup.kotlinpoet.metadata.classinspectors.ElementsClassInspector import com.squareup.moshi.JsonClass import com.squareup.moshi.kotlin.codegen.api.AdapterGenerator import com.squareup.moshi.kotlin.codegen.api.PropertyGenerator diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/MoshiCachedClassInspector.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/MoshiCachedClassInspector.kt index 756c4e4..f893921 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/MoshiCachedClassInspector.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/MoshiCachedClassInspector.kt @@ -16,12 +16,16 @@ package com.squareup.moshi.kotlin.codegen.apt import com.squareup.kotlinpoet.TypeSpec -import com.squareup.kotlinpoet.metadata.ImmutableKmClass import com.squareup.kotlinpoet.metadata.specs.ClassInspector import com.squareup.kotlinpoet.metadata.specs.toTypeSpec -import com.squareup.kotlinpoet.metadata.toImmutableKmClass +import com.squareup.kotlinpoet.metadata.toKmClass +import kotlinx.metadata.KmClass +import java.util.TreeMap import javax.lang.model.element.TypeElement +/** KmClass doesn't implement equality natively. */ +private val KmClassComparator = compareBy { it.name } + /** * This cached API over [ClassInspector] that caches certain lookups Moshi does potentially multiple * times. This is useful mostly because it avoids duplicate reloads in cases like common base @@ -29,16 +33,16 @@ import javax.lang.model.element.TypeElement */ internal class MoshiCachedClassInspector(private val classInspector: ClassInspector) { private val elementToSpecCache = mutableMapOf() - private val kmClassToSpecCache = mutableMapOf() - private val metadataToKmClassCache = mutableMapOf() + private val kmClassToSpecCache = TreeMap(KmClassComparator) + private val metadataToKmClassCache = mutableMapOf() - fun toImmutableKmClass(metadata: Metadata): ImmutableKmClass { + fun toKmClass(metadata: Metadata): KmClass { return metadataToKmClassCache.getOrPut(metadata) { - metadata.toImmutableKmClass() + metadata.toKmClass() } } - fun toTypeSpec(kmClass: ImmutableKmClass): TypeSpec { + fun toTypeSpec(kmClass: KmClass): TypeSpec { return kmClassToSpecCache.getOrPut(kmClass) { kmClass.toTypeSpec(classInspector) } @@ -46,7 +50,7 @@ internal class MoshiCachedClassInspector(private val classInspector: ClassInspec fun toTypeSpec(element: TypeElement): TypeSpec { return elementToSpecCache.getOrPut(element) { - toTypeSpec(toImmutableKmClass(element.metadata)) + toTypeSpec(toKmClass(element.metadata)) } } } diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt index 67015c4..bfd3012 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/apt/metadata.kt @@ -27,7 +27,6 @@ import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.WildcardTypeName import com.squareup.kotlinpoet.asClassName import com.squareup.kotlinpoet.asTypeName -import com.squareup.kotlinpoet.metadata.ImmutableKmConstructor import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview import com.squareup.kotlinpoet.metadata.isAbstract import com.squareup.kotlinpoet.metadata.isClass @@ -37,8 +36,8 @@ import com.squareup.kotlinpoet.metadata.isInternal import com.squareup.kotlinpoet.metadata.isLocal import com.squareup.kotlinpoet.metadata.isPublic import com.squareup.kotlinpoet.metadata.isSealed -import com.squareup.kotlinpoet.metadata.specs.TypeNameAliasTag import com.squareup.kotlinpoet.tag +import com.squareup.kotlinpoet.tags.TypeAliasTag import com.squareup.moshi.Json import com.squareup.moshi.JsonQualifier import com.squareup.moshi.kotlin.codegen.api.DelegateKey @@ -49,6 +48,8 @@ import com.squareup.moshi.kotlin.codegen.api.TargetProperty import com.squareup.moshi.kotlin.codegen.api.TargetType import com.squareup.moshi.kotlin.codegen.api.deepCopy import com.squareup.moshi.kotlin.codegen.api.rawType +import kotlinx.metadata.KmConstructor +import kotlinx.metadata.jvm.signature import java.lang.annotation.ElementType import java.lang.annotation.Retention import java.lang.annotation.RetentionPolicy @@ -101,7 +102,7 @@ internal fun primaryConstructor( ) } - val kmConstructorSignature = primaryConstructor.tag()?.signature?.toString() + val kmConstructorSignature = primaryConstructor.tag()?.signature?.toString() ?: run { messager.printMessage( ERROR, @@ -138,7 +139,7 @@ internal fun targetType( } val kmClass = try { - cachedClassInspector.toImmutableKmClass(typeMetadata) + cachedClassInspector.toKmClass(typeMetadata) } catch (e: UnsupportedOperationException) { messager.printMessage( ERROR, @@ -173,7 +174,7 @@ internal fun targetType( ) return null } - kmClass.isSealed -> { + kmClass.flags.isSealed -> { messager.printMessage( ERROR, "@JsonClass can't be applied to $element: must not be sealed", @@ -181,7 +182,7 @@ internal fun targetType( ) return null } - kmClass.isAbstract -> { + kmClass.flags.isAbstract -> { messager.printMessage( ERROR, "@JsonClass can't be applied to $element: must not be abstract", @@ -189,7 +190,7 @@ internal fun targetType( ) return null } - kmClass.isLocal -> { + kmClass.flags.isLocal -> { messager.printMessage( ERROR, "@JsonClass can't be applied to $element: must not be local", @@ -197,7 +198,7 @@ internal fun targetType( ) return null } - !kmClass.isPublic && !kmClass.isInternal -> { + !kmClass.flags.isPublic && !kmClass.flags.isInternal -> { messager.printMessage( ERROR, "@JsonClass can't be applied to $element: must be internal or public", @@ -311,8 +312,8 @@ internal fun targetType( // Implicitly public, so now look up the hierarchy val forceInternal = generateSequence(element) { it.enclosingElement } .filterIsInstance() - .map { cachedClassInspector.toImmutableKmClass(it.metadata) } - .any { it.isInternal } + .map { cachedClassInspector.toKmClass(it.metadata) } + .any { it.flags.isInternal } if (forceInternal) KModifier.INTERNAL else visibility } @@ -518,7 +519,7 @@ private fun String.escapeDollarSigns(): String { internal fun TypeName.unwrapTypeAlias(): TypeName { return when (this) { is ClassName -> { - tag()?.type?.let { unwrappedType -> + tag()?.abbreviatedType?.let { unwrappedType -> // If any type is nullable, then the whole thing is nullable var isAnyNullable = isNullable // Keep track of all annotations across type levels. Sort them too for consistency.