From 935f8b872a038a09cc0cd88d3c426933672e0266 Mon Sep 17 00:00:00 2001 From: Zac Sweers Date: Sat, 13 Mar 2021 20:47:27 -0500 Subject: [PATCH] Support generic arrays with defaults in code gen (#1310) --- buildSrc/src/main/kotlin/Dependencies.kt | 2 +- .../moshi/kotlin/codegen/api/kotlintypes.kt | 15 ++++++++++++++- .../moshi/kotlin/codegen/GeneratedAdaptersTest.kt | 4 +++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 47eaf78..ece39bb 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -53,7 +53,7 @@ object Dependencies { object Testing { const val assertj = "org.assertj:assertj-core:3.11.1" - const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.3.4" + const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.3.6" const val junit = "junit:junit:4.13.1" const val truth = "com.google.truth:truth:1.0.1" } diff --git a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt index 5dcbb91..94a5c86 100644 --- a/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt +++ b/kotlin/codegen/src/main/java/com/squareup/moshi/kotlin/codegen/api/kotlintypes.kt @@ -36,7 +36,9 @@ import com.squareup.kotlinpoet.TypeName import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.UNIT import com.squareup.kotlinpoet.WildcardTypeName +import com.squareup.kotlinpoet.asClassName import com.squareup.kotlinpoet.asTypeName +import java.lang.reflect.Array internal fun TypeName.rawType(): ClassName { return findRawType() ?: throw IllegalArgumentException("Cannot get raw type from $this") @@ -71,7 +73,18 @@ internal fun TypeName.asTypeBlock(): CodeBlock { when (this) { is ParameterizedTypeName -> { return if (rawType == ARRAY) { - CodeBlock.of("%T::class.java", copy(nullable = false)) + val componentType = typeArguments[0] + if (componentType is ParameterizedTypeName) { + // "generic" array just uses the component's raw type + // java.lang.reflect.Array.newInstance(, 0).javaClass + CodeBlock.of( + "%T.newInstance(%L, 0).javaClass", + Array::class.java.asClassName(), + componentType.rawType.asTypeBlock() + ) + } else { + CodeBlock.of("%T::class.java", copy(nullable = false)) + } } else { rawType.asTypeBlock() } diff --git a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt index 3632ca1..042b8fd 100644 --- a/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt +++ b/kotlin/tests/src/test/kotlin/com/squareup/moshi/kotlin/codegen/GeneratedAdaptersTest.kt @@ -1455,7 +1455,9 @@ data class SmokeTestType( val favoriteNullableArrayValues: Array, val nullableSetListMapArrayNullableIntWithDefault: Set>>>? = null, val aliasedName: TypeAliasName = "Woah", - val genericAlias: GenericTypeAlias = listOf("Woah") + val genericAlias: GenericTypeAlias = listOf("Woah"), + // Regression test for https://github.com/square/moshi/issues/1272 + val nestedArray: Array>? = null ) // Compile only, regression test for https://github.com/square/moshi/issues/848