Support generic arrays with defaults in code gen (#1310)

This commit is contained in:
Zac Sweers
2021-03-13 20:47:27 -05:00
committed by GitHub
parent 6df66b81dc
commit 935f8b872a
3 changed files with 18 additions and 3 deletions

View File

@@ -53,7 +53,7 @@ object Dependencies {
object Testing { object Testing {
const val assertj = "org.assertj:assertj-core:3.11.1" 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 junit = "junit:junit:4.13.1"
const val truth = "com.google.truth:truth:1.0.1" const val truth = "com.google.truth:truth:1.0.1"
} }

View File

@@ -36,7 +36,9 @@ import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.TypeVariableName import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.UNIT import com.squareup.kotlinpoet.UNIT
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 java.lang.reflect.Array
internal fun TypeName.rawType(): ClassName { internal fun TypeName.rawType(): ClassName {
return findRawType() ?: throw IllegalArgumentException("Cannot get raw type from $this") return findRawType() ?: throw IllegalArgumentException("Cannot get raw type from $this")
@@ -71,7 +73,18 @@ internal fun TypeName.asTypeBlock(): CodeBlock {
when (this) { when (this) {
is ParameterizedTypeName -> { is ParameterizedTypeName -> {
return if (rawType == ARRAY) { return if (rawType == ARRAY) {
val componentType = typeArguments[0]
if (componentType is ParameterizedTypeName) {
// "generic" array just uses the component's raw type
// java.lang.reflect.Array.newInstance(<raw-type>, 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)) CodeBlock.of("%T::class.java", copy(nullable = false))
}
} else { } else {
rawType.asTypeBlock() rawType.asTypeBlock()
} }

View File

@@ -1455,7 +1455,9 @@ data class SmokeTestType(
val favoriteNullableArrayValues: Array<String?>, val favoriteNullableArrayValues: Array<String?>,
val nullableSetListMapArrayNullableIntWithDefault: Set<List<Map<String, Array<IntArray?>>>>? = null, val nullableSetListMapArrayNullableIntWithDefault: Set<List<Map<String, Array<IntArray?>>>>? = null,
val aliasedName: TypeAliasName = "Woah", 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<Map<String, Any>>? = null
) )
// Compile only, regression test for https://github.com/square/moshi/issues/848 // Compile only, regression test for https://github.com/square/moshi/issues/848