Update to KotlinPoet 1.0.0-RC1 (#599)

* Update to kotlinpoet 1.0.0-RC1

* Move to WildcardTypeName.STAR

* simpleNames() function -> simpleNames property

* packageName() fun -> packageName property

* simpleName() fun -> simpleName property

* Check if bounds are empty for TypeVariableName. If so, use no-bounds creator

* Use new parameterizedBy/plusParameter API where appropriate
This commit is contained in:
Zac Sweers
2018-07-17 19:44:20 -07:00
committed by Jesse Wilson
parent bcd61c9621
commit 78821bbc80
7 changed files with 44 additions and 35 deletions

View File

@@ -25,7 +25,7 @@
<dependency> <dependency>
<groupId>com.squareup</groupId> <groupId>com.squareup</groupId>
<artifactId>kotlinpoet</artifactId> <artifactId>kotlinpoet</artifactId>
<version>0.7.0</version> <version>1.0.0-RC1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.google.auto</groupId> <groupId>com.google.auto</groupId>

View File

@@ -25,6 +25,7 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.NameAllocator import com.squareup.kotlinpoet.NameAllocator
import com.squareup.kotlinpoet.ParameterSpec import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.TypeVariableName
@@ -54,7 +55,7 @@ internal class AdapterGenerator(
private val typeVariables = target.typeVariables private val typeVariables = target.typeVariables
private val nameAllocator = NameAllocator() private val nameAllocator = NameAllocator()
private val adapterName = "${className.simpleNames().joinToString(separator = "_")}JsonAdapter" private val adapterName = "${className.simpleNames.joinToString(separator = "_")}JsonAdapter"
private val originalTypeName = target.element.asType().asTypeName() private val originalTypeName = target.element.asType().asTypeName()
private val moshiParam = ParameterSpec.builder( private val moshiParam = ParameterSpec.builder(
@@ -62,7 +63,7 @@ internal class AdapterGenerator(
Moshi::class).build() Moshi::class).build()
private val typesParam = ParameterSpec.builder( private val typesParam = ParameterSpec.builder(
nameAllocator.newName("types"), nameAllocator.newName("types"),
ParameterizedTypeName.get(ARRAY, Type::class.asTypeName())) ARRAY.parameterizedBy(Type::class.asTypeName()))
.build() .build()
private val readerParam = ParameterSpec.builder( private val readerParam = ParameterSpec.builder(
nameAllocator.newName("reader"), nameAllocator.newName("reader"),
@@ -76,8 +77,7 @@ internal class AdapterGenerator(
nameAllocator.newName("value"), nameAllocator.newName("value"),
originalTypeName.asNullable()) originalTypeName.asNullable())
.build() .build()
private val jsonAdapterTypeName = ParameterizedTypeName.get( private val jsonAdapterTypeName = JsonAdapter::class.asClassName().parameterizedBy(originalTypeName)
JsonAdapter::class.asClassName(), originalTypeName)
// selectName() API setup // selectName() API setup
private val optionsProperty = PropertySpec.builder( private val optionsProperty = PropertySpec.builder(
@@ -92,7 +92,7 @@ internal class AdapterGenerator(
property.allocateNames(nameAllocator) property.allocateNames(nameAllocator)
} }
val result = FileSpec.builder(className.packageName(), adapterName) val result = FileSpec.builder(className.packageName, adapterName)
result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.") result.addComment("Code generated by moshi-kotlin-codegen. Do not edit.")
companionObjectName?.let { companionObjectName?.let {
result.addFunction(generateJsonAdapterFun(it)) result.addFunction(generateJsonAdapterFun(it))
@@ -161,7 +161,7 @@ internal class AdapterGenerator(
.addModifiers(KModifier.OVERRIDE) .addModifiers(KModifier.OVERRIDE)
.returns(String::class) .returns(String::class)
.addStatement("return %S", .addStatement("return %S",
"GeneratedJsonAdapter(${originalTypeName.rawType().simpleNames().joinToString(".")})") "GeneratedJsonAdapter(${originalTypeName.rawType().simpleNames.joinToString(".")})")
.build() .build()
} }

View File

@@ -24,10 +24,12 @@ import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.NameAllocator import com.squareup.kotlinpoet.NameAllocator
import com.squareup.kotlinpoet.ParameterSpec import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.PropertySpec import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName import com.squareup.kotlinpoet.asTypeName
import com.squareup.moshi.JsonAdapter import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Types import com.squareup.moshi.Types
@@ -74,8 +76,7 @@ internal data class DelegateKey(
val adapterName = nameAllocator.newName( val adapterName = nameAllocator.newName(
"${type.toVariableName().decapitalize()}${qualifierNames}Adapter", this) "${type.toVariableName().decapitalize()}${qualifierNames}Adapter", this)
val adapterTypeName = ParameterizedTypeName.get( val adapterTypeName = JsonAdapter::class.asClassName().parameterizedBy(type)
JsonAdapter::class.asTypeName(), type)
val qualifiers = jsonQualifiers val qualifiers = jsonQualifiers
val standardArgs = arrayOf(moshiParameter, val standardArgs = arrayOf(moshiParameter,
if (type is ClassName && qualifiers.isEmpty()) { if (type is ClassName && qualifiers.isEmpty()) {
@@ -114,8 +115,8 @@ private fun List<TypeName>.toVariableNames() = joinToString("") { it.toVariableN
/** Returns a suggested variable name derived from a type name, like nullableListOfString. */ /** Returns a suggested variable name derived from a type name, like nullableListOfString. */
private fun TypeName.toVariableName(): String { private fun TypeName.toVariableName(): String {
val base = when (this) { val base = when (this) {
is ClassName -> simpleName() is ClassName -> simpleName
is ParameterizedTypeName -> rawType.simpleName() + "Of" + typeArguments.toVariableNames() is ParameterizedTypeName -> rawType.simpleName + "Of" + typeArguments.toVariableNames()
is WildcardTypeName -> (lowerBounds + upperBounds).toVariableNames() is WildcardTypeName -> (lowerBounds + upperBounds).toVariableNames()
is TypeVariableName -> name + bounds.toVariableNames() is TypeVariableName -> name + bounds.toVariableNames()
else -> throw IllegalArgumentException("Unrecognized type! $this") else -> throw IllegalArgumentException("Unrecognized type! $this")

View File

@@ -204,13 +204,19 @@ internal data class TargetType(
private fun genericTypeNames(proto: Class, nameResolver: NameResolver): List<TypeVariableName> { private fun genericTypeNames(proto: Class, nameResolver: NameResolver): List<TypeVariableName> {
return proto.typeParameterList.map { return proto.typeParameterList.map {
val possibleBounds = it.upperBoundList
.map { it.asTypeName(nameResolver, proto::getTypeParameter, false) }
val typeVar = if (possibleBounds.isEmpty()) {
TypeVariableName( TypeVariableName(
name = nameResolver.getString(it.name), name = nameResolver.getString(it.name),
bounds = *(it.upperBoundList
.map { it.asTypeName(nameResolver, proto::getTypeParameter, false) }
.toTypedArray()),
variance = it.varianceModifier) variance = it.varianceModifier)
.reified(it.reified) } else {
TypeVariableName(
name = nameResolver.getString(it.name),
bounds = *possibleBounds.toTypedArray(),
variance = it.varianceModifier)
}
return@map typeVar.reified(it.reified)
} }
} }

View File

@@ -20,6 +20,7 @@ import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
/** /**
* Resolves type parameters against a type declaration. Use this to fill in type variables with * Resolves type parameters against a type declaration. Use this to fill in type variables with
@@ -33,8 +34,7 @@ open class TypeResolver {
is ClassName -> typeName is ClassName -> typeName
is ParameterizedTypeName -> { is ParameterizedTypeName -> {
ParameterizedTypeName.get( typeName.rawType.parameterizedBy(*(typeName.typeArguments.map { resolve(it) }.toTypedArray()))
typeName.rawType, *(typeName.typeArguments.map { resolve(it) }.toTypedArray()))
.asNullableIf(typeName.nullable) .asNullableIf(typeName.nullable)
} }

View File

@@ -18,7 +18,7 @@ package com.squareup.moshi.kotlin.codegen
import com.squareup.kotlinpoet.ANY import com.squareup.kotlinpoet.ANY
import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterizedTypeName import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName import com.squareup.kotlinpoet.WildcardTypeName
@@ -31,15 +31,20 @@ internal fun TypeParameter.asTypeName(
nameResolver: NameResolver, nameResolver: NameResolver,
getTypeParameter: (index: Int) -> TypeParameter, getTypeParameter: (index: Int) -> TypeParameter,
resolveAliases: Boolean = false resolveAliases: Boolean = false
): TypeName { ): TypeVariableName {
return TypeVariableName( val possibleBounds = upperBoundList.map {
name = nameResolver.getString(name),
bounds = *(upperBoundList.map {
it.asTypeName(nameResolver, getTypeParameter, resolveAliases) it.asTypeName(nameResolver, getTypeParameter, resolveAliases)
} }
.toTypedArray()), return if (possibleBounds.isEmpty()) {
variance = variance.asKModifier() TypeVariableName(
) name = nameResolver.getString(name),
variance = variance.asKModifier())
} else {
TypeVariableName(
name = nameResolver.getString(name),
bounds = *possibleBounds.toTypedArray(),
variance = variance.asKModifier())
}
} }
internal fun TypeParameter.Variance.asKModifier(): KModifier? { internal fun TypeParameter.Variance.asKModifier(): KModifier? {
@@ -112,16 +117,16 @@ internal fun Type.asTypeName(
WildcardTypeName.subtypeOf(argumentTypeName) WildcardTypeName.subtypeOf(argumentTypeName)
} }
} }
Type.Argument.Projection.STAR -> WildcardTypeName.subtypeOf(ANY) Type.Argument.Projection.STAR -> WildcardTypeName.STAR
Type.Argument.Projection.INV -> TODO("INV projection is unsupported") Type.Argument.Projection.INV -> TODO("INV projection is unsupported")
} }
} ?: argumentTypeName } ?: argumentTypeName
} }
} else { } else {
WildcardTypeName.subtypeOf(ANY) WildcardTypeName.STAR
} }
}.toTypedArray() }.toTypedArray()
typeName = ParameterizedTypeName.get(typeName as ClassName, *remappedArgs) typeName = (typeName as ClassName).parameterizedBy(*remappedArgs)
} }
return typeName.asNullableIf(nullable) return typeName.asNullableIf(nullable)

View File

@@ -16,10 +16,9 @@
package com.squareup.moshi.kotlin.codegen package com.squareup.moshi.kotlin.codegen
import com.google.common.truth.Truth.assertThat import com.google.common.truth.Truth.assertThat
import com.squareup.kotlinpoet.ParameterizedTypeName import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.plusParameter
import com.squareup.kotlinpoet.WildcardTypeName import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.asClassName import com.squareup.kotlinpoet.asClassName
import com.squareup.moshi.kotlin.codegen.TypeResolver
import org.junit.Test import org.junit.Test
class TypeResolverTest { class TypeResolverTest {
@@ -32,9 +31,7 @@ class TypeResolverTest {
@Test @Test
fun ensureParameterizedNullabilityIsPreserved() { fun ensureParameterizedNullabilityIsPreserved() {
val nullableTypeName = ParameterizedTypeName.get( val nullableTypeName = List::class.plusParameter(String::class)
List::class.asClassName(),
String::class.asClassName())
.asNullable() .asNullable()
assertThat(resolver.resolve(nullableTypeName).nullable).isTrue() assertThat(resolver.resolve(nullableTypeName).nullable).isTrue()