Some miscellaneous kotlinpoet updates (#786)

* KotlinPoet 1.0.1

* Nix asNullableIf in favor of plain copy() function use

* Add a test for packages that look like classes

Resolves #783
This commit is contained in:
Zac Sweers 2019-01-13 07:18:00 -08:00 committed by Jesse Wilson
parent 3588c98b86
commit ded3bccc60
5 changed files with 17 additions and 12 deletions

View file

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

View file

@ -35,18 +35,18 @@ open class TypeResolver {
is ParameterizedTypeName -> {
typeName.rawType.parameterizedBy(*(typeName.typeArguments.map { resolve(it) }.toTypedArray()))
.asNullableIf(typeName.isNullable)
.copy(nullable = typeName.isNullable)
}
is WildcardTypeName -> {
when {
typeName.inTypes.size == 1 -> {
WildcardTypeName.consumerOf(resolve(typeName.inTypes[0]))
.asNullableIf(typeName.isNullable)
.copy(nullable = typeName.isNullable)
}
typeName.outTypes.size == 1 -> {
WildcardTypeName.producerOf(resolve(typeName.outTypes[0]))
.asNullableIf(typeName.isNullable)
.copy(nullable = typeName.isNullable)
}
else -> {
throw IllegalArgumentException(

View file

@ -26,7 +26,3 @@ internal fun TypeName.rawType(): ClassName {
else -> throw IllegalArgumentException("Cannot get raw type from $this")
}
}
internal fun TypeName.asNullableIf(condition: Boolean): TypeName {
return if (condition) copy(nullable = true) else this
}

View file

@ -77,17 +77,17 @@ internal fun Type.asTypeName(
if (hasFlexibleUpperBound()) {
return WildcardTypeName.producerOf(
flexibleUpperBound.asTypeName(nameResolver, getTypeParameter, useAbbreviatedType))
.asNullableIf(nullable)
.copy(nullable = nullable)
} else if (hasOuterType()) {
return WildcardTypeName.consumerOf(
outerType.asTypeName(nameResolver, getTypeParameter, useAbbreviatedType))
.asNullableIf(nullable)
.copy(nullable = nullable)
}
val realType = when {
hasTypeParameter() -> return getTypeParameter(typeParameter)
.asTypeName(nameResolver, getTypeParameter, useAbbreviatedType)
.asNullableIf(nullable)
.copy(nullable = nullable)
hasTypeParameterName() -> typeParameterName
useAbbreviatedType && hasAbbreviatedType() -> abbreviatedType.typeAliasName
else -> className
@ -121,5 +121,5 @@ internal fun Type.asTypeName(
typeName = (typeName as ClassName).parameterizedBy(*remappedArgs)
}
return typeName.asNullableIf(nullable)
return typeName.copy(nullable = nullable)
}

View file

@ -0,0 +1,9 @@
package com.squareup.moshi.kotlin.codgen.LooksLikeAClass
import com.squareup.moshi.JsonClass
/**
* https://github.com/square/moshi/issues/783
*/
@JsonClass(generateAdapter = true)
data class ClassInPackageThatLooksLikeAClass(val foo: String)