-
-
Notifications
You must be signed in to change notification settings - Fork 331
/
ValueClassSupport.kt
85 lines (70 loc) · 2.43 KB
/
ValueClassSupport.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package io.mockk
import kotlin.reflect.KClass
import kotlin.reflect.KProperty1
import kotlin.reflect.full.declaredMemberProperties
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.isAccessible
private val valueClassFieldCache = mutableMapOf<KClass<out Any>, KProperty1<out Any, *>>()
/**
* Get boxed value of any value class
*
* @return boxed value of value class, if this is value class, else just itself
*/
fun <T : Any> T.boxedValue(): Any? {
if (!this::class.isValueClass()) return this
// get backing field
val backingField = this::class.valueField()
// get boxed value
@Suppress("UNCHECKED_CAST")
return (backingField as KProperty1<T, *>).get(this)
}
/**
* Get class of boxed value of any value class
*
* @return class of boxed value, if this is value class, else just class of itself
*/
fun <T : Any> T.boxedClass(): KClass<*> {
return this::class.boxedClass()
}
/**
* Get the KClass of boxed value if this is a value class.
*
* @return class of boxed value, if this is value class, else just class of itself
*/
fun KClass<*>.boxedClass(): KClass<*> {
if (!this.isValueClass()) return this
// get backing field
val backingField = this.valueField()
// get boxed value
return backingField.returnType.classifier as KClass<*>
}
private fun <T : Any> KClass<T>.valueField(): KProperty1<out T, *> {
@Suppress("UNCHECKED_CAST")
return valueClassFieldCache.getOrPut(this) {
require(isValue) { "$this is not a value class" }
// value classes always have a primary constructor...
val constructor = primaryConstructor!!
// ...and exactly one constructor parameter
val constructorParameter = constructor.parameters.first()
// ...with a backing field
val backingField = declaredMemberProperties
.first { it.name == constructorParameter.name }
.apply { isAccessible = true }
backingField
} as KProperty1<out T, *>
}
private fun <T : Any> KClass<T>.isValueClass() = try {
this.isValue
} catch (_: UnsupportedOperationException) {
false
}
/**
* POLYFILL for kotlin version < 1.5
* will be shadowed by implementation in kotlin SDK 1.5+
*
* @return true if this is an inline class, else false
*/
private val <T : Any> KClass<T>.isValue: Boolean
get() = !isData &&
primaryConstructor?.parameters?.size == 1 &&
java.declaredMethods.any { it.name == "box-impl" }