-
Notifications
You must be signed in to change notification settings - Fork 53
/
ProvidesDescriptor.java
334 lines (285 loc) · 8.81 KB
/
ProvidesDescriptor.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
/*
* Copyright (c) 2020 TechEmpower. All rights reserved.
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.extras.provides;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import jakarta.inject.Named;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.DescriptorType;
import org.glassfish.hk2.api.DescriptorVisibility;
import org.glassfish.hk2.api.HK2Loader;
import org.glassfish.hk2.api.Injectee;
import org.glassfish.hk2.api.ProxyForSameScope;
import org.glassfish.hk2.api.Rank;
import org.glassfish.hk2.api.ServiceHandle;
import org.glassfish.hk2.api.UseProxy;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.hk2.utilities.reflection.ReflectionHelper;
/**
* An {@link ActiveDescriptor} implementation used by {@link ProvidesListener}
* and {@link NoInstancesService}.
*/
final class ProvidesDescriptor<T> implements ActiveDescriptor<T> {
private final ReentrantLock lock = new ReentrantLock();
private final AnnotatedElement annotatedElement;
private final Class<?> implementationClass;
private final Type implementationType;
private final Set<Type> contracts;
private final Annotation scopeAnnotation;
private final Function<ServiceHandle<?>, T> createFunction;
private final Consumer<T> disposeFunction;
ProvidesDescriptor(AnnotatedElement annotatedElement,
Class<?> implementationClass,
Type implementationType,
Set<Type> contracts,
Annotation scopeAnnotation,
Function<ServiceHandle<?>, T> createFunction,
Consumer<T> disposeFunction) {
this.annotatedElement = Objects.requireNonNull(annotatedElement);
this.implementationClass = Objects.requireNonNull(implementationClass);
this.implementationType = Objects.requireNonNull(implementationType);
this.contracts = Collections.unmodifiableSet(new HashSet<>(Objects.requireNonNull(contracts)));
this.scopeAnnotation = Objects.requireNonNull(scopeAnnotation);
this.createFunction = Objects.requireNonNull(createFunction);
this.disposeFunction = Objects.requireNonNull(disposeFunction);
}
@Override
public T create(ServiceHandle<?> root) {
return createFunction.apply(root);
}
@Override
public void dispose(T instance) {
disposeFunction.accept(instance);
}
@Override
public Class<?> getImplementationClass() {
return implementationClass;
}
@Override
public Type getImplementationType() {
return implementationType;
}
@Override
public Set<Type> getContractTypes() {
return contracts;
}
@Override
public boolean isReified() {
return true;
}
@Override
public Annotation getScopeAsAnnotation() {
return scopeAnnotation;
}
@Override
public Class<? extends Annotation> getScopeAnnotation() {
return getScopeAsAnnotation().annotationType();
}
@Override
public Set<Annotation> getQualifierAnnotations() {
return Collections.unmodifiableSet(
ReflectionHelper.getQualifierAnnotations(annotatedElement));
}
@Override
public List<Injectee> getInjectees() {
return Collections.emptyList();
}
@Override
public /*@Nullable*/ Long getFactoryServiceId() {
return null;
}
@Override
public /*@Nullable*/ Long getFactoryLocatorId() {
return null;
}
@Override
public String getImplementation() {
return getImplementationClass().getName();
}
@Override
public Set<String> getAdvertisedContracts() {
return getContractTypes()
.stream()
.map(contract -> ReflectionHelper.getRawClass(contract))
.filter(contractClass -> contractClass != null)
.map(contractClass -> contractClass.getName())
.collect(
Collectors.collectingAndThen(
Collectors.toSet(),
set -> Collections.unmodifiableSet(set)));
}
@Override
public String getScope() {
return getScopeAnnotation().getName();
}
@Override
public /*@Nullable*/ String getName() {
return Arrays.stream(annotatedElement.getAnnotations())
.filter(annotation -> annotation.annotationType() == Named.class)
.map(annotation -> ((Named) annotation))
.map(annotation -> annotation.value())
.findAny()
.orElse(null);
}
@Override
public Set<String> getQualifiers() {
return getQualifierAnnotations()
.stream()
.map(annotation -> annotation.annotationType())
.map(annotationType -> annotationType.getName())
.collect(
Collectors.collectingAndThen(
Collectors.toSet(),
set -> Collections.unmodifiableSet(set)));
}
@Override
public DescriptorType getDescriptorType() {
return DescriptorType.CLASS;
}
@Override
public DescriptorVisibility getDescriptorVisibility() {
return DescriptorVisibility.NORMAL;
}
@Override
public Map<String, List<String>> getMetadata() {
Map<String, List<String>> metadata = new HashMap<>();
Annotation scope = getScopeAsAnnotation();
BuilderHelper.getMetadataValues(scope, metadata);
for (Annotation qualifier : getQualifierAnnotations())
BuilderHelper.getMetadataValues(qualifier, metadata);
metadata.replaceAll((key, values) -> Collections.unmodifiableList(values));
return Collections.unmodifiableMap(metadata);
}
@Override
public /*@Nullable*/ HK2Loader getLoader() {
return null;
}
/*@GuardedBy("this")*/
private int ranking = 0;
/*@GuardedBy("this")*/
private boolean initialRankingFound = false;
@Override
public int getRanking() {
lock.lock();
try {
if (!initialRankingFound) {
Rank rank = annotatedElement.getAnnotation(Rank.class);
if (rank != null)
ranking = rank.value();
initialRankingFound = true;
}
return ranking;
} finally {
lock.unlock();
}
}
@Override
public int setRanking(int ranking) {
lock.lock();
try {
int previousRanking = getRanking();
this.ranking = ranking;
return previousRanking;
} finally {
lock.unlock();
}
}
@Override
public /*@Nullable*/ Boolean isProxiable() {
UseProxy useProxy = annotatedElement.getAnnotation(UseProxy.class);
return (useProxy == null) ? null : useProxy.value();
}
@Override
public /*@Nullable*/ Boolean isProxyForSameScope() {
ProxyForSameScope proxyForSameScope =
annotatedElement.getAnnotation(ProxyForSameScope.class);
return (proxyForSameScope == null) ? null : proxyForSameScope.value();
}
@Override
public /*@Nullable*/ String getClassAnalysisName() {
return null;
}
@Override
public /*@Nullable*/ Long getServiceId() {
return null;
}
@Override
public /*@Nullable*/ Long getLocatorId() {
return null;
}
/*@GuardedBy("this")*/
private /*@Nullable*/ T cache = null;
/*@GuardedBy("this")*/
private boolean isCacheSet = false;
@Override
public /*@Nullable*/ T getCache() {
lock.lock();
try {
if (!isCacheSet)
throw new IllegalStateException();
return cache;
} finally {
lock.unlock();
}
}
@Override
public boolean isCacheSet() {
lock.lock();
try {
return isCacheSet;
} finally {
lock.unlock();
}
}
@Override
public void setCache(T cacheMe) {
lock.lock();
try {
cache = cacheMe;
isCacheSet = true;
} finally {
lock.unlock();
}
}
@Override
public void releaseCache() {
lock.lock();
try {
cache = null;
isCacheSet = false;
} finally {
lock.unlock();
}
}
@Override
public String toString() {
return getClass().getSimpleName() + "[" + annotatedElement + "]";
}
}