/
ProxyMockMaker.java
172 lines (151 loc) · 6.4 KB
/
ProxyMockMaker.java
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
/*
* Copyright (c) 2021 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.proxy;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.invocation.RealMethod;
import org.mockito.internal.invocation.SerializableMethod;
import org.mockito.internal.util.Platform;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.MockMaker;
import java.lang.reflect.*;
import java.util.concurrent.atomic.AtomicReference;
import static org.mockito.internal.invocation.DefaultInvocationFactory.createInvocation;
import static org.mockito.internal.util.StringUtil.join;
/**
* A mock maker that is using the {@link Proxy} utility and is therefore only capable of mocking interfaces but
* does not rely on manual byte code generation but only uses official and public Java API.
*/
public class ProxyMockMaker implements MockMaker {
private final Method invokeDefault;
public ProxyMockMaker() {
Method m;
try {
m =
InvocationHandler.class.getMethod(
"invokeDefault", Object.class, Method.class, Object[].class);
} catch (NoSuchMethodException ignored) {
m = null;
}
invokeDefault = m;
}
@Override
@SuppressWarnings("unchecked")
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<?>[] ifaces = new Class<?>[settings.getExtraInterfaces().size() + 1];
ifaces[0] = settings.getTypeToMock();
int index = 1;
for (Class<?> iface : settings.getExtraInterfaces()) {
ifaces[index++] = iface;
}
return (T)
Proxy.newProxyInstance(
settings.getTypeToMock().getClassLoader(),
ifaces,
new MockInvocationHandler(handler, settings));
}
@Override
public MockHandler getHandler(Object mock) {
if (!Proxy.isProxyClass(mock.getClass())) {
return null;
}
InvocationHandler handler = Proxy.getInvocationHandler(mock);
if (!(handler instanceof MockInvocationHandler)) {
return null;
}
return ((MockInvocationHandler) handler).handler.get();
}
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
((MockInvocationHandler) Proxy.getInvocationHandler(mock)).handler.set(newHandler);
}
@Override
public TypeMockability isTypeMockable(Class<?> type) {
return new TypeMockability() {
@Override
public boolean mockable() {
return type.isInterface();
}
@Override
public String nonMockableReason() {
return mockable() ? "" : "non-interface";
}
};
}
private class MockInvocationHandler implements InvocationHandler {
private final AtomicReference<MockHandler<?>> handler;
private final MockCreationSettings<?> settings;
private MockInvocationHandler(MockHandler<?> handler, MockCreationSettings<?> settings) {
this.handler = new AtomicReference<>(handler);
this.settings = settings;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getDeclaringClass() == Object.class) {
switch (method.getName()) {
case "hashCode":
return System.identityHashCode(proxy);
case "equals":
return proxy == args[0];
case "toString":
return "";
default:
throw new MockitoException(
join(
"Unexpected overridable method of Object class found",
"",
"The method "
+ method
+ " was not expected to be declared. Either your JVM build offers "
+ "non-official API or the current functionality is not supported",
Platform.describe()));
}
}
RealMethod realMethod;
if (invokeDefault == null || Modifier.isAbstract(method.getModifiers())) {
realMethod = RealMethod.IsIllegal.INSTANCE;
} else {
realMethod = new RealDefaultMethod(proxy, method, args);
}
return handler.get()
.handle(
createInvocation(
proxy, method, args, realMethod, settings, new LocationImpl()));
}
}
private class RealDefaultMethod implements RealMethod {
private final Object proxy;
private final SerializableMethod serializableMethod;
private final Object[] args;
private RealDefaultMethod(Object proxy, Method method, Object[] args) {
this.proxy = proxy;
this.serializableMethod = new SerializableMethod(method);
this.args = args;
}
@Override
public boolean isInvokable() {
return true;
}
@Override
public Object invoke() throws Throwable {
try {
return invokeDefault.invoke(null, proxy, serializableMethod.getJavaMethod(), args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new MockitoException(
join(
"Failed to access default method or invoked method with illegal arguments",
"",
"Method "
+ serializableMethod.getJavaMethod()
+ " could not be delegated, this is not supposed to happen",
Platform.describe()),
e);
}
}
}
}