-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
JvmMockFactoryHelper.kt
163 lines (135 loc) · 4.9 KB
/
JvmMockFactoryHelper.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package io.mockk.impl.instantiation
import io.mockk.*
import io.mockk.impl.InternalPlatform
import io.mockk.impl.stub.Stub
import io.mockk.proxy.MockKInvocationHandler
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.util.concurrent.Callable
import kotlin.coroutines.Continuation
import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty
import kotlin.reflect.KParameter
import kotlin.reflect.full.memberProperties
import kotlin.reflect.jvm.javaField
import kotlin.reflect.jvm.javaMethod
import kotlin.reflect.jvm.kotlinFunction
object JvmMockFactoryHelper {
fun mockHandler(stub: Stub) = object : MockKInvocationHandler {
override fun invocation(self: Any, method: Method?, originalCall: Callable<*>?, args: Array<Any?>) =
stdFunctions(self, method!!, args) {
stub.handleInvocation(
self,
method.toDescription(), {
handleOriginalCall(originalCall, method)
},
args,
findBackingField(self, method)
)
}
}
private fun findBackingField(self: Any, method: Method): BackingFieldValueProvider {
return {
val property = self::class.memberProperties.firstOrNull {
it.getter.javaMethod == method ||
(it is KMutableProperty<*> && it.setter.javaMethod == method)
}
property?.javaField?.let { field ->
BackingFieldValue(
property.name,
{
InternalPlatformDsl.makeAccessible(field);
field.get(self)
},
{
InternalPlatformDsl.makeAccessible(field);
field.set(self, it)
}
)
}
}
}
private inline fun stdFunctions(
self: Any,
method: Method,
args: Array<Any?>,
otherwise: () -> Any?
): Any? {
if (self is Class<*>) {
if (method.isHashCode()) {
return System.identityHashCode(self)
} else if (method.isEquals()) {
return self === args[0]
}
}
return otherwise()
}
private fun handleOriginalCall(originalMethod: Callable<*>?, method: Method): Any? {
if (originalMethod == null) {
throw MockKException("No way to call original method ${method.toDescription()}")
}
return try {
originalMethod.call()
} catch (ex: InvocationTargetException) {
throw ex.cause ?: throw ex
}
}
internal fun Method.toDescription(): MethodDescription {
val cached = cache[this]
if (cached != null) return cached
val kotlinFunc =
try {
// workaround for
// https://github.com/mockk/mockk/issues/18
// https://github.com/mockk/mockk/issues/22
kotlinFunction
} catch (ex: Throwable) {
null
}
val vararg = when {
kotlinFunc != null ->
kotlinFunc.parameters
.filter { it.kind != KParameter.Kind.INSTANCE }
.indexOfFirst { it.isVararg }
isVarArgs ->
parameterTypes.size - 1
else -> -1
}
val returnTypeIsUnit = when {
kotlinFunc != null ->
kotlinFunc.returnType.toString() == "kotlin.Unit"
else ->
returnType == Void.TYPE
}
val returnTypeIsNothing =
kotlinFunc?.returnType?.toString() == "kotlin.Nothing"
val isSuspend = when {
kotlinFunc != null ->
kotlinFunc.isSuspend
else -> parameterTypes.lastOrNull()?.let {
Continuation::class.java.isAssignableFrom(it)
} ?: false
}
val isFnCall = Function::class.java.isAssignableFrom(declaringClass)
val returnType = kotlinFunc?.returnType as? KClass<*> ?: returnType.kotlin
val result = MethodDescription(
name,
returnType,
returnTypeIsUnit,
returnTypeIsNothing,
isSuspend,
isFnCall,
declaringClass.kotlin,
parameterTypes.map { it.kotlin },
vararg,
Modifier.isPrivate(modifiers) ||
Modifier.isProtected(modifiers)
)
cache[this] = result
return result
}
fun Method.isHashCode() = name == "hashCode" && parameterTypes.isEmpty()
fun Method.isEquals() = name == "equals" && parameterTypes.size == 1 && parameterTypes[0] === Object::class.java
val cache = InternalPlatform.weakMap<Method, MethodDescription>()
}