Use javaObjectType instead of Java primitives while creating parametrized type (#731)

* Add test for collection of primitives

* Use javaObjectType instead of Java primitives while creating parametrized type
This commit is contained in:
Artem Daugel-Dauge 2018-11-04 20:42:51 +03:00 committed by Zac Sweers
parent 1acc70dd70
commit 0e3a52b729
2 changed files with 29 additions and 25 deletions

View file

@ -30,7 +30,6 @@ import com.squareup.kotlinpoet.SHORT
import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.asTypeName
import com.squareup.moshi.Types
/**
@ -43,17 +42,7 @@ abstract class TypeRenderer {
fun render(typeName: TypeName): CodeBlock {
if (typeName.nullable) {
if (typeName == BOOLEAN.asNullable()
|| typeName == BYTE.asNullable()
|| typeName == CHAR.asNullable()
|| typeName == DOUBLE.asNullable()
|| typeName == FLOAT.asNullable()
|| typeName == INT.asNullable()
|| typeName == LONG.asNullable()
|| typeName == SHORT.asNullable()) {
return CodeBlock.of("%T::class.javaObjectType", typeName.asNonNull())
}
return render(typeName.asNonNull())
return renderObjectType(typeName.asNonNull())
}
return when (typeName) {
@ -64,7 +53,7 @@ abstract class TypeRenderer {
if (typeName.rawType == ARRAY) {
CodeBlock.of("%T.arrayOf(%L)",
Types::class,
render(typeName.typeArguments[0].objectType()))
renderObjectType(typeName.typeArguments[0]))
} else {
val builder = CodeBlock.builder().apply {
add("%T.", Types::class)
@ -74,9 +63,9 @@ abstract class TypeRenderer {
} else {
add("newParameterizedType(")
}
add("%T::class.java", typeName.rawType.objectType())
add("%T::class.java", typeName.rawType)
for (typeArgument in typeName.typeArguments) {
add(", %L", render(typeArgument.objectType()))
add(", %L", renderObjectType(typeArgument))
}
add(")")
}
@ -108,17 +97,18 @@ abstract class TypeRenderer {
}
}
private fun TypeName.objectType(): TypeName {
private fun renderObjectType(typeName: TypeName): CodeBlock {
return if (typeName.isPrimitive()) {
CodeBlock.of("%T::class.javaObjectType", typeName)
} else {
render(typeName)
}
}
private fun TypeName.isPrimitive(): Boolean {
return when (this) {
BOOLEAN -> Boolean::class.javaObjectType.asTypeName()
BYTE -> Byte::class.javaObjectType.asTypeName()
SHORT -> Short::class.javaObjectType.asTypeName()
INT -> Integer::class.javaObjectType.asTypeName()
LONG -> Long::class.javaObjectType.asTypeName()
CHAR -> Character::class.javaObjectType.asTypeName()
FLOAT -> Float::class.javaObjectType.asTypeName()
DOUBLE -> Double::class.javaObjectType.asTypeName()
else -> this
BOOLEAN, BYTE, SHORT, INT, LONG, CHAR, FLOAT, DOUBLE -> true
else -> false
}
}
}

View file

@ -1112,6 +1112,20 @@ class GeneratedAdaptersTest {
val decoded = adapter.fromJson("""{"a":null}""")!!
assertThat(decoded.a).isEqualTo(null)
}
@JsonClass(generateAdapter = true)
data class HasCollectionOfPrimitives(val listOfInts: List<Int>)
@Test fun hasCollectionOfPrimitives() {
val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(HasCollectionOfPrimitives::class.java)
val encoded = HasCollectionOfPrimitives(listOf(1, 2, -3))
assertThat(adapter.toJson(encoded)).isEqualTo("""{"listOfInts":[1,2,-3]}""")
val decoded = adapter.fromJson("""{"listOfInts":[4,-5,6]}""")!!
assertThat(decoded).isEqualTo(HasCollectionOfPrimitives(listOf(4, -5, 6)))
}
}
// Has to be outside to avoid Types seeing an owning class