mirror of
https://github.com/fankes/moshi.git
synced 2025-10-20 00:19:21 +08:00
Support generic arrays with defaults in code gen (#1310)
This commit is contained in:
@@ -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"
|
||||||
}
|
}
|
||||||
|
@@ -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()
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user