1
1
/*
2
- * Copyright 2020 Appmattus Limited
2
+ * Copyright 2020-2023 Appmattus Limited
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -19,14 +19,28 @@ package com.appmattus.kotlinfixture.resolver
19
19
import com.appmattus.kotlinfixture.Context
20
20
import com.appmattus.kotlinfixture.Unresolved
21
21
import com.appmattus.kotlinfixture.Unresolved.Companion.createUnresolved
22
+ import com.appmattus.kotlinfixture.typeOf
22
23
import kotlin.reflect.KClass
23
24
import kotlin.reflect.KType
24
25
import kotlin.reflect.full.starProjectedType
25
26
26
27
internal class ArrayKTypeResolver : Resolver {
27
28
28
29
@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
+
30
44
if (obj is KType && obj.classifier?.starProjectedType == Array <Any ?>::class .starProjectedType) {
31
45
val size = context.configuration.repeatCount()
32
46
@@ -48,4 +62,15 @@ internal class ArrayKTypeResolver : Resolver {
48
62
49
63
return Unresolved .Unhandled
50
64
}
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
+ }
51
76
}
0 commit comments