-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
IsolationScheme.java
280 lines (260 loc) · 12.5 KB
/
IsolationScheme.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
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.isolated;
import org.gradle.api.file.ArchiveOperations;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.problems.Problems;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.services.BuildServiceRegistry;
import org.gradle.api.specs.Spec;
import org.gradle.internal.Cast;
import org.gradle.internal.logging.text.TreeFormatter;
import org.gradle.internal.reflect.Types;
import org.gradle.internal.reflect.Types.TypeVisitResult;
import org.gradle.internal.service.ServiceLookup;
import org.gradle.internal.service.ServiceLookupException;
import org.gradle.internal.service.UnknownServiceException;
import org.gradle.process.ExecOperations;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicReference;
public class IsolationScheme<IMPLEMENTATION, PARAMS> {
private final Class<IMPLEMENTATION> interfaceType;
private final Class<PARAMS> paramsType;
private final Class<? extends PARAMS> noParamsType;
public IsolationScheme(Class<IMPLEMENTATION> interfaceType, Class<PARAMS> paramsType, Class<? extends PARAMS> noParamsType) {
this.interfaceType = interfaceType;
this.paramsType = paramsType;
this.noParamsType = noParamsType;
}
/**
* Determines the parameters type for the given implementation.
*
* @return The parameters type, or {@code null} when the implementation takes no parameters.
*/
@Nullable
public <T extends IMPLEMENTATION, P extends PARAMS> Class<P> parameterTypeFor(Class<T> implementationType) {
return parameterTypeFor(implementationType, 0);
}
/**
* Determines the parameters type found at the given type argument index for the given implementation.
*
* @return The parameters type, or {@code null} when the implementation takes no parameters.
*/
@Nullable
public <T extends IMPLEMENTATION, P extends PARAMS> Class<P> parameterTypeFor(Class<T> implementationType, int typeArgumentIndex) {
if (implementationType == interfaceType) {
return null;
}
Class<P> parametersType = inferParameterType(implementationType, typeArgumentIndex);
if (parametersType == paramsType) {
TreeFormatter formatter = new TreeFormatter();
formatter.node("Could not create the parameters for ");
formatter.appendType(implementationType);
formatter.append(": must use a sub-type of ");
formatter.appendType(parametersType);
formatter.append(" as the parameters type. Use ");
formatter.appendType(noParamsType);
formatter.append(" as the parameters type for implementations that do not take parameters.");
throw new IllegalArgumentException(formatter.toString());
}
if (parametersType == noParamsType) {
return null;
}
return parametersType;
}
/**
* Walk the type hierarchy until we find the interface type and keep track the chain of the type parameters.
*
* E.g.: For `interface Baz<T>`, interface `Bar<T extends CharSequence> extends Baz<T>` and `class Foo implements Bar<String>`,
* we'll have mapping `T extends CharSequence -> String` and `T -> String`.
*
* When we come to `Baz<T>`, we can then query the mapping for `T` and get `String`.
*/
@Nonnull
private <T extends IMPLEMENTATION, P extends PARAMS> Class<P> inferParameterType(Class<T> implementationType, int typeArgumentIndex) {
AtomicReference<Type> foundType = new AtomicReference<>();
Map<Type, Type> collectedTypes = new HashMap<>();
Types.walkTypeHierarchy(implementationType, type -> {
for (Type genericInterface : type.getGenericInterfaces()) {
if (collectTypeParameters(genericInterface, foundType, collectedTypes, typeArgumentIndex)) {
return TypeVisitResult.TERMINATE;
}
}
Type genericSuperclass = type.getGenericSuperclass();
if (collectTypeParameters(genericSuperclass, foundType, collectedTypes, typeArgumentIndex)) {
return TypeVisitResult.TERMINATE;
}
return TypeVisitResult.CONTINUE;
});
// Note: we don't handle GenericArrayType here, since
// we don't support arrays as a type of a Parameter anywhere
Type type = unwrapTypeVariable(foundType.get());
return type instanceof Class
? Cast.uncheckedNonnullCast(type)
: type instanceof ParameterizedType
? Cast.uncheckedNonnullCast(((ParameterizedType) type).getRawType())
: Cast.uncheckedNonnullCast(paramsType);
}
private boolean collectTypeParameters(Type type, AtomicReference<Type> foundType, Map<Type, Type> collectedTypeParameters, int typeArgumentIndex) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getRawType().equals(interfaceType)) {
Type parameter = parameterizedType.getActualTypeArguments()[typeArgumentIndex];
foundType.set(collectedTypeParameters.getOrDefault(parameter, parameter));
return true;
}
Type[] actualTypes = parameterizedType.getActualTypeArguments();
Type[] typeParameters = ((Class<?>) parameterizedType.getRawType()).getTypeParameters();
for (int i = 0; i < typeParameters.length; i++) {
Type firstActualInTypeChain = collectedTypeParameters.getOrDefault(actualTypes[i], actualTypes[i]);
collectedTypeParameters.put(typeParameters[i], firstActualInTypeChain);
}
}
return false;
}
private Type unwrapTypeVariable(Type type) {
if (type instanceof TypeVariable) {
Type nextType;
Queue<Type> queue = new ArrayDeque<>();
queue.add(type);
while ((nextType = queue.poll()) != null) {
for (Type bound : ((TypeVariable<?>) nextType).getBounds()) {
if (bound instanceof TypeVariable) {
queue.add(bound);
} else if (isAssignableFromType(paramsType, bound)) {
return bound;
}
}
}
}
return type;
}
private static boolean isAssignableFromType(Class<?> clazz, Type type) {
return (type instanceof Class && clazz.isAssignableFrom((Class<?>) type))
|| (type instanceof ParameterizedType && clazz.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType()));
}
/**
* Returns the services available for injection into the implementation instance.
*/
public ServiceLookup servicesForImplementation(@Nullable PARAMS params, ServiceLookup allServices) {
return servicesForImplementation(params, allServices, Collections.emptyList(), c -> false);
}
/**
* Returns the services available for injection into the implementation instance.
*/
public ServiceLookup servicesForImplementation(@Nullable PARAMS params, ServiceLookup allServices, Collection<? extends Class<?>> additionalWhiteListedServices, Spec<Class<?>> whiteListPolicy) {
return new ServicesForIsolatedObject(interfaceType, noParamsType, params, allServices, additionalWhiteListedServices, whiteListPolicy);
}
private static class ServicesForIsolatedObject implements ServiceLookup {
private final Class<?> interfaceType;
private final Class<?> noParamsType;
private final Collection<? extends Class<?>> additionalWhiteListedServices;
private final ServiceLookup allServices;
private final Object params;
private final Spec<Class<?>> whiteListPolicy;
public ServicesForIsolatedObject(
Class<?> interfaceType,
Class<?> noParamsType,
@Nullable Object params,
ServiceLookup allServices,
Collection<? extends Class<?>> additionalWhiteListedServices,
Spec<Class<?>> whiteListPolicy
) {
this.interfaceType = interfaceType;
this.noParamsType = noParamsType;
this.additionalWhiteListedServices = additionalWhiteListedServices;
this.allServices = allServices;
this.params = params;
this.whiteListPolicy = whiteListPolicy;
}
@Nullable
@Override
public Object find(Type serviceType) throws ServiceLookupException {
if (serviceType instanceof Class) {
Class<?> serviceClass = Cast.uncheckedNonnullCast(serviceType);
if (serviceClass.isInstance(params)) {
return params;
}
if (serviceClass.isAssignableFrom(noParamsType)) {
throw new ServiceLookupException(String.format("Cannot query the parameters of an instance of %s that takes no parameters.", interfaceType.getSimpleName()));
}
if (serviceClass.isAssignableFrom(ExecOperations.class)) {
return allServices.find(ExecOperations.class);
}
if (serviceClass.isAssignableFrom(FileSystemOperations.class)) {
return allServices.find(FileSystemOperations.class);
}
if (serviceClass.isAssignableFrom(ArchiveOperations.class)) {
return allServices.find(ArchiveOperations.class);
}
if (serviceClass.isAssignableFrom(ObjectFactory.class)) {
return allServices.find(ObjectFactory.class);
}
if (serviceClass.isAssignableFrom(ProviderFactory.class)) {
return allServices.find(ProviderFactory.class);
}
if (serviceClass.isAssignableFrom(BuildServiceRegistry.class)) {
return allServices.find(BuildServiceRegistry.class);
}
if (serviceClass.isAssignableFrom(Problems.class)) {
return allServices.find(Problems.class);
}
for (Class<?> whiteListedService : additionalWhiteListedServices) {
if (serviceClass.isAssignableFrom(whiteListedService)) {
return allServices.find(whiteListedService);
}
}
if (whiteListPolicy.isSatisfiedBy(serviceClass)) {
return allServices.find(serviceClass);
}
}
return null;
}
@Override
public Object get(Type serviceType) throws UnknownServiceException, ServiceLookupException {
Object result = find(serviceType);
if (result == null) {
return notFound(serviceType);
}
return result;
}
@Override
public Object get(Type serviceType, Class<? extends Annotation> annotatedWith) throws UnknownServiceException, ServiceLookupException {
return notFound(serviceType);
}
private Object notFound(Type serviceType) {
TreeFormatter formatter = new TreeFormatter();
formatter.node("Services of type ");
formatter.appendType(serviceType);
formatter.append(" are not available for injection into instances of type ");
formatter.appendType(interfaceType);
formatter.append(".");
throw new UnknownServiceException(serviceType, formatter.toString());
}
}
}