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

View File

@@ -1112,6 +1112,20 @@ class GeneratedAdaptersTest {
val decoded = adapter.fromJson("""{"a":null}""")!! val decoded = adapter.fromJson("""{"a":null}""")!!
assertThat(decoded.a).isEqualTo(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 // Has to be outside to avoid Types seeing an owning class