Skip to content

Commit 6749aa8

Browse files
authored
Ensure Arrays of primitives are correctly generated. (#97)
Works around issues in Kotlins type system. See https://youtrack.jetbrains.com/issue/KT-52170/Reflection-typeOfArrayLong-gives-classifier-LongArray Fixes #92
1 parent 3e579d1 commit 6749aa8

File tree

4 files changed

+416
-13
lines changed

4 files changed

+416
-13
lines changed

fixture/src/main/kotlin/com/appmattus/kotlinfixture/resolver/ArrayKTypeResolver.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Appmattus Limited
2+
* Copyright 2020-2023 Appmattus Limited
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,14 +19,28 @@ package com.appmattus.kotlinfixture.resolver
1919
import com.appmattus.kotlinfixture.Context
2020
import com.appmattus.kotlinfixture.Unresolved
2121
import com.appmattus.kotlinfixture.Unresolved.Companion.createUnresolved
22+
import com.appmattus.kotlinfixture.typeOf
2223
import kotlin.reflect.KClass
2324
import kotlin.reflect.KType
2425
import kotlin.reflect.full.starProjectedType
2526

2627
internal class ArrayKTypeResolver : Resolver {
2728

2829
@Suppress("ReturnCount")
29-
override fun resolve(context: Context, obj: Any): Any? {
30+
override fun resolve(context: Context, obj: Any): Any {
31+
// Special handling for primitive arrays as Kotlin's KType seems to see Array<Int> as IntArray etc when looking at the classifier
32+
// See https://youtrack.jetbrains.com/issue/KT-52170/Reflection-typeOfArrayLong-gives-classifier-LongArray
33+
when (obj) {
34+
BooleanArrayKType -> return (context.resolve(BooleanArray::class) as BooleanArray).toTypedArray()
35+
ByteArrayKType -> return (context.resolve(ByteArray::class) as ByteArray).toTypedArray()
36+
DoubleArrayKType -> return (context.resolve(DoubleArray::class) as DoubleArray).toTypedArray()
37+
FloatArrayKType -> return (context.resolve(FloatArray::class) as FloatArray).toTypedArray()
38+
IntArrayKType -> return (context.resolve(IntArray::class) as IntArray).toTypedArray()
39+
LongArrayKType -> return (context.resolve(LongArray::class) as LongArray).toTypedArray()
40+
ShortArrayKType -> return (context.resolve(ShortArray::class) as ShortArray).toTypedArray()
41+
CharArrayKType -> return (context.resolve(CharArray::class) as CharArray).toTypedArray()
42+
}
43+
3044
if (obj is KType && obj.classifier?.starProjectedType == Array<Any?>::class.starProjectedType) {
3145
val size = context.configuration.repeatCount()
3246

@@ -48,4 +62,15 @@ internal class ArrayKTypeResolver : Resolver {
4862

4963
return Unresolved.Unhandled
5064
}
65+
66+
companion object {
67+
private val BooleanArrayKType = typeOf<Array<Boolean>>()
68+
private val ByteArrayKType = typeOf<Array<Byte>>()
69+
private val DoubleArrayKType = typeOf<Array<Double>>()
70+
private val FloatArrayKType = typeOf<Array<Float>>()
71+
private val IntArrayKType = typeOf<Array<Int>>()
72+
private val LongArrayKType = typeOf<Array<Long>>()
73+
private val ShortArrayKType = typeOf<Array<Short>>()
74+
private val CharArrayKType = typeOf<Array<Char>>()
75+
}
5176
}

0 commit comments

Comments
 (0)