/
Advice.kt
185 lines (154 loc) · 5.52 KB
/
Advice.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package io.mockk.proxy.android.advice
import io.mockk.core.ValueClassSupport.boxedValue
import io.mockk.proxy.MockKAgentException
import io.mockk.proxy.android.AndroidMockKMap
import io.mockk.proxy.android.MethodDescriptor
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.lang.reflect.Modifier.isFinal
import java.lang.reflect.Modifier.isPublic
import java.util.concurrent.Callable
internal class Advice(
private val handlers: AndroidMockKMap,
private val staticHandlers: AndroidMockKMap,
private val constructorHandlers: AndroidMockKMap
) {
private val selfCallInfo = SelfCallInfo()
@Suppress("unused") // called from dispatcher
fun getOrigin(instance: Any?, methodWithTypeAndSignature: String): Method? {
val methodDesc = MethodDescriptor(methodWithTypeAndSignature)
val obj = instance
?: MethodDescriptor.classForTypeName(methodDesc.className)
if (!obj.checkSelfCall()) {
return null
}
if (instance != null && instance::class.java.isOverridden(methodDesc.method)) {
return null
}
return methodDesc.method
}
@Suppress("unused") // called from dispatcher
fun handle(
instance: Any,
origin: Method,
arguments: Array<Any?>
): Callable<*>? {
if (isInternalHashMap(instance)) {
return null;
}
val instanceOrClass =
if (Modifier.isStatic(origin.modifiers)) {
val methodDesc = MethodDescriptor(instance as String)
MethodDescriptor.classForTypeName(methodDesc.className)
} else {
instance
}
val handler =
handlers[instanceOrClass]
?: staticHandlers[instanceOrClass]
?: constructorHandlers[instanceOrClass]
?: return null
val superMethodCall = SuperMethodCall(
selfCallInfo,
origin,
instanceOrClass,
arguments
)
return Callable {
handler.invocation(
instanceOrClass,
origin,
superMethodCall,
arguments
)
?.boxedValue // unbox value class objects
}
}
private fun isInternalHashMap(instance: Any) =
handlers.isInternalHashMap(instance) ||
staticHandlers.isInternalHashMap(instance) ||
constructorHandlers.isInternalHashMap(instance)
@Suppress("unused") // called from dispatcher
fun handleConstructor(
instance: Any,
methodDescriptor: String,
arguments: Array<Any?>
): Callable<*>? {
val methodDesc = MethodDescriptor(methodDescriptor)
val cls = MethodDescriptor.classForTypeName(methodDesc.className)
val handler =
constructorHandlers[cls]
?: return null
return Callable {
handler.invocation(
instance,
null,
null,
arguments
)
}
}
@Suppress("unused") // called from dispatcher
fun isMock(instance: Any): Boolean {
if (isInternalHashMap(instance)) {
return false;
}
return handlers.containsKey(instance) ||
staticHandlers.containsKey(instance) ||
constructorHandlers.containsKey(instance)
}
private fun Any.checkSelfCall() = selfCallInfo.checkSelfCall(this)
private class SuperMethodCall(
private val selfCallInfo: SelfCallInfo,
private val origin: Method,
private val instance: Any,
private val arguments: Array<Any?>
) : Callable<Any?> {
override fun call(): Any? = try {
origin.makeAccessible()
selfCallInfo.set(instance)
origin.invoke(instance, *arguments)
} catch (exception: InvocationTargetException) {
throw exception.cause
?: MockKAgentException("no cause for InvocationTargetException", exception)
}
}
private class SelfCallInfo : ThreadLocal<Any>() {
fun checkSelfCall(value: Any) =
if (get() === value) {
set(null)
false
} else {
true
}
}
companion object {
private tailrec fun Class<*>?.isOverridden(origin: Method): Boolean {
if (this == null) return false
val method = findMethod(origin.name, origin.parameterTypes)
?: return superclass.isOverridden(origin)
return origin.declaringClass !== method.declaringClass
}
private fun Class<*>.findMethod(name: String, parameters: Array<Class<*>>) =
declaredMethods.firstOrNull {
it.name == name &&
it.parameterTypes refEquals parameters
}
private infix fun <T> Array<T>.refEquals(other: Array<T>) =
size == other.size && zip(other).none { (a, b) -> a !== b }
private val Class<*>.final
get() = isFinal(modifiers)
private val Method.final
get() = isFinal(modifiers)
private fun Method.makeAccessible() {
if (!isPublic(declaringClass.modifiers and this.modifiers)) {
isAccessible = true
}
}
}
}