Update KotlinPoet to 1.10.0 (#1396)

This commit is contained in:
Zac Sweers
2021-09-20 16:13:56 -04:00
committed by GitHub
parent 628a193d69
commit de8bbf12f5
5 changed files with 31 additions and 37 deletions

View File

@@ -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"

View File

@@ -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)

View File

@@ -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

View File

@@ -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<KmClass> { 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<TypeElement, TypeSpec>()
private val kmClassToSpecCache = mutableMapOf<ImmutableKmClass, TypeSpec>()
private val metadataToKmClassCache = mutableMapOf<Metadata, ImmutableKmClass>()
private val kmClassToSpecCache = TreeMap<KmClass, TypeSpec>(KmClassComparator)
private val metadataToKmClassCache = mutableMapOf<Metadata, KmClass>()
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))
}
}
}

View File

@@ -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<ImmutableKmConstructor>()?.signature?.toString()
val kmConstructorSignature = primaryConstructor.tag<KmConstructor>()?.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>(element) { it.enclosingElement }
.filterIsInstance<TypeElement>()
.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<TypeNameAliasTag>()?.type?.let { unwrappedType ->
tag<TypeAliasTag>()?.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.