/
SubclassInjectionLoader.java
157 lines (134 loc) · 6.25 KB
/
SubclassInjectionLoader.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
/*
* Copyright (c) 2017 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.creation.bytebuddy;
import static org.mockito.internal.util.StringUtil.join;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.utility.GraalImageCode;
import org.mockito.codegen.InjectionBase;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.Platform;
class SubclassInjectionLoader implements SubclassLoader {
private static final String ERROR_MESSAGE =
join(
"The current JVM does not support any class injection mechanism.",
"",
"Currently, Mockito supports injection via either by method handle lookups or using sun.misc.Unsafe",
"Neither seems to be available on your current JVM.");
private final SubclassLoader loader;
SubclassInjectionLoader() {
if (!Boolean.parseBoolean(
System.getProperty(
"org.mockito.internal.noUnsafeInjection",
Boolean.toString(GraalImageCode.getCurrent().isDefined())))
&& ClassInjector.UsingReflection.isAvailable()) {
this.loader = new WithReflection();
} else if (GraalImageCode.getCurrent().isDefined()) {
this.loader = new WithIsolatedLoader();
} else if (ClassInjector.UsingLookup.isAvailable()) {
this.loader = tryLookup();
} else {
throw new MockitoException(join(ERROR_MESSAGE, "", Platform.describe()));
}
}
private static SubclassLoader tryLookup() {
try {
Class<?> methodHandles = Class.forName("java.lang.invoke.MethodHandles");
Object lookup = methodHandles.getMethod("lookup").invoke(null);
Method privateLookupIn =
methodHandles.getMethod(
"privateLookupIn",
Class.class,
Class.forName("java.lang.invoke.MethodHandles$Lookup"));
Object codegenLookup = privateLookupIn.invoke(null, InjectionBase.class, lookup);
return new WithLookup(lookup, codegenLookup, privateLookupIn);
} catch (Exception exception) {
throw new MockitoException(join(ERROR_MESSAGE, "", Platform.describe()), exception);
}
}
private static class WithReflection implements SubclassLoader {
@Override
public boolean isDisrespectingOpenness() {
return true;
}
@Override
public ClassLoadingStrategy<ClassLoader> resolveStrategy(
Class<?> mockedType, ClassLoader classLoader, boolean localMock) {
return ClassLoadingStrategy.Default.INJECTION.with(
localMock
? mockedType.getProtectionDomain()
: InjectionBase.class.getProtectionDomain());
}
}
private static class WithIsolatedLoader implements SubclassLoader {
@Override
public boolean isDisrespectingOpenness() {
return false;
}
@Override
public ClassLoadingStrategy<ClassLoader> resolveStrategy(
Class<?> mockedType, ClassLoader classLoader, boolean localMock) {
return ClassLoadingStrategy.Default.WRAPPER;
}
}
private static class WithLookup implements SubclassLoader {
private final Object lookup;
private final Object codegenLookup;
private final Method privateLookupIn;
WithLookup(Object lookup, Object codegenLookup, Method privateLookupIn) {
this.lookup = lookup;
this.codegenLookup = codegenLookup;
this.privateLookupIn = privateLookupIn;
}
@Override
public boolean isDisrespectingOpenness() {
return false;
}
@Override
public ClassLoadingStrategy<ClassLoader> resolveStrategy(
Class<?> mockedType, ClassLoader classLoader, boolean localMock) {
if (localMock) {
try {
Object privateLookup;
try {
privateLookup = privateLookupIn.invoke(null, mockedType, lookup);
} catch (InvocationTargetException exception) {
if (exception.getCause() instanceof IllegalAccessException) {
return ClassLoadingStrategy.Default.WRAPPER.with(
mockedType.getProtectionDomain());
} else {
throw exception.getCause();
}
}
return ClassLoadingStrategy.UsingLookup.of(privateLookup);
} catch (Throwable exception) {
throw new MockitoException(
join(
"The Java module system prevents Mockito from defining a mock class in the same package as "
+ mockedType,
"",
"To overcome this, you must open and export the mocked type to Mockito.",
"Remember that you can also do so programmatically if the mocked class is defined by the same module as your test code",
exception));
}
} else if (classLoader == InjectionBase.class.getClassLoader()) {
return ClassLoadingStrategy.UsingLookup.of(codegenLookup);
} else {
return ClassLoadingStrategy.Default.WRAPPER.with(mockedType.getProtectionDomain());
}
}
}
@Override
public boolean isDisrespectingOpenness() {
return loader.isDisrespectingOpenness();
}
@Override
public ClassLoadingStrategy<ClassLoader> resolveStrategy(
Class<?> mockedType, ClassLoader classLoader, boolean localMock) {
return loader.resolveStrategy(mockedType, classLoader, localMock);
}
}