/
InlineByteBuddyMockMaker.java
481 lines (448 loc) · 21.8 KB
/
InlineByteBuddyMockMaker.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright (c) 2016 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import net.bytebuddy.agent.ByteBuddyAgent;
import org.mockito.Incubating;
import org.mockito.creation.instance.Instantiator;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.base.MockitoInitializationException;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.util.Platform;
import org.mockito.internal.util.concurrent.DetachedThreadLocal;
import org.mockito.internal.util.concurrent.WeakConcurrentMap;
import org.mockito.invocation.MockHandler;
import org.mockito.mock.MockCreationSettings;
import org.mockito.plugins.InlineMockMaker;
import static org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.*;
import static org.mockito.internal.util.StringUtil.*;
/**
* Agent and subclass based mock maker.
* <p>
* This mock maker which uses a combination of the Java instrumentation API and sub-classing rather than creating
* a new sub-class to create a mock. This way, it becomes possible to mock final types and methods. This mock
* maker <strong>must to be activated explicitly</strong> for supporting mocking final types and methods:
* <p>
* <p>
* This mock maker can be activated by creating the file <code>/mockito-extensions/org.mockito.plugins.MockMaker</code>
* containing the text <code>mock-maker-inline</code> or <code>org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker</code>.
* <p>
* <p>
* This mock maker will make a best effort to avoid subclass creation when creating a mock. Otherwise it will use the
* <code>org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker</code> to create the mock class. That means
* that the following condition is true
* <p>
* <pre class="code"><code class="java">
* class Foo { }
* assert mock(Foo.class).getClass() == Foo.class;
* </pre></code>
* <p>
* unless any of the following conditions is met, in such case the mock maker <em>fall backs</em> to the
* the creation of a subclass.
* <p>
* <ul>
* <li>the type to mock is an abstract class.</li>
* <li>the mock is set to require additional interfaces.</li>
* <li>the mock is <a href="#20">explicitly set to support serialization</a>.</li>
* </ul>
* <p>
* <p>
* Some type of the JDK cannot be mocked, this includes <code>Class</code>, <code>String</code>, and wrapper types.
* <p>
* <p>
* Nevertheless, final methods of such types are mocked when using the inlining mock maker. Mocking final types and enums
* does however remain impossible when explicitly requiring serialization support or when adding ancillary interfaces.
* <p>
* <p>
* Important behavioral changes when using inline-mocks:
* <ul>
* <li>Mockito is capable of mocking package-private methods even if they are defined in different packages than
* the mocked type. Mockito voluntarily never mocks package-visible methods within <code>java.*</code> packages.</li>
* <li>Additionally to final types, Mockito can now mock types that are not visible for extension; such types
* include private types in a protected package.</li>
* <li>Mockito can no longer mock <code>native</code> methods. Inline mocks require byte code manipulation of a
* method where native methods do not offer any byte code to manipulate.</li>
* <li>Mockito cannot longer strip <code>synchronized</code> modifiers from mocked instances.</li>
* </ul>
* <p>
* <p>
* Note that inline mocks require a Java agent to be attached. Mockito will attempt an attachment of a Java agent upon
* loading the mock maker for creating inline mocks. Such runtime attachment is only possible when using a JVM that
* is part of a JDK or when using a Java 9 VM. When running on a non-JDK VM prior to Java 9, it is however possible to
* manually add the <a href="http://bytebuddy.net">Byte Buddy Java agent jar</a> using the <code>-javaagent</code>
* parameter upon starting the JVM. Furthermore, the inlining mock maker requires the VM to support class retransformation
* (also known as HotSwap). All major VM distributions such as HotSpot (OpenJDK), J9 (IBM/Websphere) or Zing (Azul)
* support this feature.
*/
@Incubating
public class InlineByteBuddyMockMaker implements ClassCreatingMockMaker, InlineMockMaker {
private static final Instrumentation INSTRUMENTATION;
private static final Throwable INITIALIZATION_ERROR;
static {
Instrumentation instrumentation;
Throwable initializationError = null;
try {
try {
instrumentation = ByteBuddyAgent.install();
if (!instrumentation.isRetransformClassesSupported()) {
throw new IllegalStateException(
join(
"Byte Buddy requires retransformation for creating inline mocks. This feature is unavailable on the current VM.",
"",
"You cannot use this mock maker on this VM"));
}
File boot = File.createTempFile("mockitoboot", ".jar");
boot.deleteOnExit();
JarOutputStream outputStream = new JarOutputStream(new FileOutputStream(boot));
try {
String source =
"org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher";
InputStream inputStream =
InlineByteBuddyMockMaker.class
.getClassLoader()
.getResourceAsStream(source + ".raw");
if (inputStream == null) {
throw new IllegalStateException(
join(
"The MockMethodDispatcher class file is not locatable: "
+ source
+ ".raw",
"",
"The class loader responsible for looking up the resource: "
+ InlineByteBuddyMockMaker.class.getClassLoader()));
}
outputStream.putNextEntry(new JarEntry(source + ".class"));
try {
int length;
byte[] buffer = new byte[1024];
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
} finally {
inputStream.close();
}
outputStream.closeEntry();
} finally {
outputStream.close();
}
try (JarFile jarfile = new JarFile(boot)) {
instrumentation.appendToBootstrapClassLoaderSearch(jarfile);
}
try {
Class.forName(
"org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher",
false,
null);
} catch (ClassNotFoundException cnfe) {
throw new IllegalStateException(
join(
"Mockito failed to inject the MockMethodDispatcher class into the bootstrap class loader",
"",
"It seems like your current VM does not support the instrumentation API correctly."),
cnfe);
}
} catch (IOException ioe) {
throw new IllegalStateException(
join(
"Mockito could not self-attach a Java agent to the current VM. This feature is required for inline mocking.",
"This error occured due to an I/O error during the creation of this agent: "
+ ioe,
"",
"Potentially, the current VM does not support the instrumentation API correctly"),
ioe);
}
} catch (Throwable throwable) {
instrumentation = null;
initializationError = throwable;
}
INSTRUMENTATION = instrumentation;
INITIALIZATION_ERROR = initializationError;
}
private final BytecodeGenerator bytecodeGenerator;
private final WeakConcurrentMap<Object, MockMethodInterceptor> mocks =
new WeakConcurrentMap.WithInlinedExpunction<Object, MockMethodInterceptor>();
private final DetachedThreadLocal<Map<Class<?>, MockMethodInterceptor>> mockedStatics =
new DetachedThreadLocal<>(DetachedThreadLocal.Cleaner.INLINE);
public InlineByteBuddyMockMaker() {
if (INITIALIZATION_ERROR != null) {
String detail;
if (System.getProperty("java.specification.vendor", "")
.toLowerCase()
.contains("android")) {
detail =
"It appears as if you are trying to run this mock maker on Android which does not support the instrumentation API.";
} else {
try {
if (Class.forName("javax.tools.ToolProvider")
.getMethod("getSystemJavaCompiler")
.invoke(null)
== null) {
detail =
"It appears as if you are running on a JRE. Either install a JDK or add JNA to the class path.";
} else {
detail =
"It appears as if your JDK does not supply a working agent attachment mechanism.";
}
} catch (Throwable ignored) {
detail =
"It appears as if you are running an incomplete JVM installation that might not support all tooling APIs";
}
}
throw new MockitoInitializationException(
join(
"Could not initialize inline Byte Buddy mock maker.",
"",
detail,
Platform.describe()),
INITIALIZATION_ERROR);
}
bytecodeGenerator =
new TypeCachingBytecodeGenerator(
new InlineBytecodeGenerator(INSTRUMENTATION, mocks, mockedStatics), true);
}
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<? extends T> type = createMockType(settings);
Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
try {
T instance = instantiator.newInstance(type);
MockMethodInterceptor mockMethodInterceptor =
new MockMethodInterceptor(handler, settings);
mocks.put(instance, mockMethodInterceptor);
if (instance instanceof MockAccess) {
((MockAccess) instance).setMockitoInterceptor(mockMethodInterceptor);
}
return instance;
} catch (org.mockito.creation.instance.InstantiationException e) {
throw new MockitoException(
"Unable to create mock instance of type '" + type.getSimpleName() + "'", e);
}
}
@Override
public <T> Class<? extends T> createMockType(MockCreationSettings<T> settings) {
try {
return bytecodeGenerator.mockClass(
MockFeatures.withMockFeatures(
settings.getTypeToMock(),
settings.getExtraInterfaces(),
settings.getSerializableMode(),
settings.isStripAnnotations()));
} catch (Exception bytecodeGenerationFailed) {
throw prettifyFailure(settings, bytecodeGenerationFailed);
}
}
private <T> RuntimeException prettifyFailure(
MockCreationSettings<T> mockFeatures, Exception generationFailed) {
if (mockFeatures.getTypeToMock().isArray()) {
throw new MockitoException(
join("Arrays cannot be mocked: " + mockFeatures.getTypeToMock() + ".", ""),
generationFailed);
}
if (Modifier.isFinal(mockFeatures.getTypeToMock().getModifiers())) {
throw new MockitoException(
join(
"Mockito cannot mock this class: " + mockFeatures.getTypeToMock() + ".",
"Can not mock final classes with the following settings :",
" - explicit serialization (e.g. withSettings().serializable())",
" - extra interfaces (e.g. withSettings().extraInterfaces(...))",
"",
"You are seeing this disclaimer because Mockito is configured to create inlined mocks.",
"You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.",
"",
"Underlying exception : " + generationFailed),
generationFailed);
}
if (Modifier.isPrivate(mockFeatures.getTypeToMock().getModifiers())) {
throw new MockitoException(
join(
"Mockito cannot mock this class: " + mockFeatures.getTypeToMock() + ".",
"Most likely it is a private class that is not visible by Mockito",
"",
"You are seeing this disclaimer because Mockito is configured to create inlined mocks.",
"You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.",
""),
generationFailed);
}
throw new MockitoException(
join(
"Mockito cannot mock this class: " + mockFeatures.getTypeToMock() + ".",
"",
"If you're not sure why you're getting this error, please report to the mailing list.",
"",
Platform.warnForVM(
"IBM J9 VM",
"Early IBM virtual machine are known to have issues with Mockito, please upgrade to an up-to-date version.\n",
"Hotspot",
Platform.isJava8BelowUpdate45()
? "Java 8 early builds have bugs that were addressed in Java 1.8.0_45, please update your JDK!\n"
: ""),
Platform.describe(),
"",
"You are seeing this disclaimer because Mockito is configured to create inlined mocks.",
"You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.",
"",
"Underlying exception : " + generationFailed),
generationFailed);
}
@Override
public MockHandler getHandler(Object mock) {
MockMethodInterceptor interceptor;
if (mock instanceof Class<?>) {
Map<Class<?>, MockMethodInterceptor> interceptors = mockedStatics.get();
interceptor = interceptors != null ? interceptors.get(mock) : null;
} else {
interceptor = mocks.get(mock);
}
if (interceptor == null) {
return null;
} else {
return interceptor.handler;
}
}
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
MockMethodInterceptor mockMethodInterceptor =
new MockMethodInterceptor(newHandler, settings);
if (mock instanceof Class<?>) {
Map<Class<?>, MockMethodInterceptor> interceptors = mockedStatics.get();
if (interceptors == null || !interceptors.containsKey(mock)) {
throw new MockitoException(
"Cannot reset "
+ mock
+ " which is not currently registered as a static mock");
}
interceptors.put((Class<?>) mock, mockMethodInterceptor);
} else {
if (!mocks.containsKey(mock)) {
throw new MockitoException(
"Cannot reset " + mock + " which is not currently registered as a mock");
}
mocks.put(mock, mockMethodInterceptor);
if (mock instanceof MockAccess) {
((MockAccess) mock).setMockitoInterceptor(mockMethodInterceptor);
}
}
}
@Override
public void clearMock(Object mock) {
if (mock instanceof Class<?>) {
for (Map<Class<?>, ?> entry : mockedStatics.getBackingMap().target.values()) {
entry.remove(mock);
}
} else {
mocks.remove(mock);
}
}
@Override
public void clearAllMocks() {
mockedStatics.getBackingMap().clear();
mocks.clear();
}
@Override
public TypeMockability isTypeMockable(final Class<?> type) {
return new TypeMockability() {
@Override
public boolean mockable() {
return INSTRUMENTATION.isModifiableClass(type) && !EXCLUDES.contains(type);
}
@Override
public String nonMockableReason() {
if (mockable()) {
return "";
}
if (type.isPrimitive()) {
return "primitive type";
}
if (EXCLUDES.contains(type)) {
return "Cannot mock wrapper types, String.class or Class.class";
}
return "VM does not support modification of given type";
}
};
}
@Override
public <T> StaticMockControl<T> createStaticMock(
Class<T> type, MockCreationSettings<T> settings, MockHandler handler) {
if (type == ConcurrentHashMap.class) {
throw new MockitoException(
"It is not possible to mock static methods of ConcurrentHashMap "
+ "to avoid infinitive loops within Mockito's implementation of static mock handling");
} else if (type == Thread.class
|| type == System.class
|| type == Arrays.class
|| ClassLoader.class.isAssignableFrom(type)) {
throw new MockitoException(
"It is not possible to mock static methods of "
+ type.getTypeName()
+ " to avoid interfering with class loading what leads to infinite loops");
}
bytecodeGenerator.mockClassStatic(type);
Map<Class<?>, MockMethodInterceptor> interceptors = mockedStatics.get();
if (interceptors == null) {
interceptors = new WeakHashMap<>();
mockedStatics.set(interceptors);
}
return new InlineStaticMockControl<>(type, interceptors, settings, handler);
}
private static class InlineStaticMockControl<T> implements StaticMockControl<T> {
private final Class<T> type;
private final Map<Class<?>, MockMethodInterceptor> interceptors;
private final MockCreationSettings<T> settings;
private final MockHandler handler;
private InlineStaticMockControl(
Class<T> type,
Map<Class<?>, MockMethodInterceptor> interceptors,
MockCreationSettings<T> settings,
MockHandler handler) {
this.type = type;
this.interceptors = interceptors;
this.settings = settings;
this.handler = handler;
}
@Override
public Class<T> getType() {
return type;
}
@Override
public void enable() {
if (interceptors.putIfAbsent(type, new MockMethodInterceptor(handler, settings))
!= null) {
throw new MockitoException(
join(
"For "
+ type.getName()
+ ", static mocking is already registered in the current thread",
"",
"To create a new mock, the existing static mock registration must be deregistered"));
}
}
@Override
public void disable() {
if (interceptors.remove(type) == null) {
throw new MockitoException(
join(
"Could not deregister "
+ type.getName()
+ " as a static mock since it is not currently registered",
"",
"To register a static mock, use Mockito.mockStatic("
+ type.getSimpleName()
+ ".class)"));
}
}
}
}