-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
ProxyMockMaker.java
184 lines (163 loc) · 6.77 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
173
174
175
176
177
178
179
180
181
182
183
184
/*
* 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.LocationFactory;
import org.mockito.internal.invocation.RealMethod;
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.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
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 static final Object[] EMPTY = new Object[0];
private final ProxyRealMethod proxyRealMethod = ProxyRealMethod.make();
@Override
@SuppressWarnings("unchecked")
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
boolean object = settings.getTypeToMock() == Object.class;
Class<?>[] ifaces = new Class<?>[settings.getExtraInterfaces().size() + (object ? 0 : 1)];
int index = 0;
if (!object) {
ifaces[index++] = settings.getTypeToMock();
}
ClassLoader classLoader = settings.getTypeToMock().getClassLoader();
for (Class<?> iface : settings.getExtraInterfaces()) {
ifaces[index++] = iface;
classLoader = resolveCommonClassLoader(classLoader, iface);
}
return (T)
Proxy.newProxyInstance(
resolveCommonClassLoader(classLoader, ProxyMockMaker.class),
ifaces,
new MockInvocationHandler(handler, settings));
}
private static ClassLoader resolveCommonClassLoader(ClassLoader mostSpecific, Class<?> type) {
if (mostSpecific == null) {
return type.getClassLoader();
}
ClassLoader candidate = type.getClassLoader();
if (candidate == null || mostSpecific == candidate) {
return mostSpecific;
}
while (candidate != null) {
if (candidate == mostSpecific) {
return type.getClassLoader();
}
candidate = candidate.getParent();
}
candidate = mostSpecific;
while (candidate != null) {
if (candidate == type.getClassLoader()) {
return mostSpecific;
}
candidate = candidate.getParent();
}
return new CommonClassLoader(mostSpecific, type.getClassLoader());
}
@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() || type == Object.class;
}
@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 (args == null) {
args = EMPTY;
}
if (method.getDeclaringClass() == Object.class) {
switch (method.getName()) {
case "hashCode":
return System.identityHashCode(proxy);
case "equals":
return proxy == args[0];
case "toString":
break;
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 (Modifier.isAbstract(method.getModifiers())) {
realMethod = RealMethod.IsIllegal.INSTANCE;
} else {
realMethod = proxyRealMethod.resolve(proxy, method, args);
}
return handler.get()
.handle(
createInvocation(
proxy,
method,
args,
realMethod,
settings,
LocationFactory.create()));
}
}
private static class CommonClassLoader extends ClassLoader {
private final ClassLoader left, right;
private CommonClassLoader(ClassLoader left, ClassLoader right) {
super(null);
this.left = left;
this.right = right;
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
return left.loadClass(name);
} catch (ClassNotFoundException ignored) {
return right.loadClass(name);
}
}
}
}