Skip to content

Commit 075b9b4

Browse files
committed
WIP
1 parent ee9316c commit 075b9b4

23 files changed

+250
-33
lines changed

impl/src/main/java/org/jboss/weld/bean/attributes/BeanAttributesFactory.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static class BeanAttributesBuilder<T> {
6767

6868
private MergedStereotypes<T, ?> mergedStereotypes;
6969
private boolean alternative;
70+
private boolean reserve;
7071
private String name;
7172
private Set<Annotation> qualifiers;
7273
private Set<Type> types;
@@ -79,6 +80,7 @@ public BeanAttributesBuilder(EnhancedAnnotated<T, ?> annotated, Set<Type> types,
7980
this.annotated = annotated;
8081
initStereotypes(annotated, manager);
8182
initAlternative(annotated);
83+
initReserve(annotated);
8284
initName(annotated);
8385
initQualifiers(annotated);
8486
initScope(annotated);
@@ -97,6 +99,10 @@ protected void initAlternative(EnhancedAnnotated<T, ?> annotated) {
9799
this.alternative = Beans.isAlternative(annotated, mergedStereotypes);
98100
}
99101

102+
protected void initReserve(EnhancedAnnotated<T, ?> annotated) {
103+
this.reserve = Beans.isReserve(annotated, mergedStereotypes);
104+
}
105+
100106
/**
101107
* Initializes the name
102108
*/
@@ -234,7 +240,8 @@ protected boolean initScopeFromStereotype() {
234240
}
235241

236242
public BeanAttributes<T> build() {
237-
return new ImmutableBeanAttributes<T>(mergedStereotypes.getStereotypes(), alternative, name, qualifiers, types,
243+
return new ImmutableBeanAttributes<T>(mergedStereotypes.getStereotypes(), alternative, reserve, name, qualifiers,
244+
types,
238245
scope);
239246
}
240247
}

impl/src/main/java/org/jboss/weld/bean/attributes/ExternalBeanAttributesFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private ExternalBeanAttributesFactory() {
5252
public static <T> BeanAttributes<T> of(BeanAttributes<T> source, BeanManager manager) {
5353
validateBeanAttributes(source, manager);
5454
BeanAttributes<T> attributes = new ImmutableBeanAttributes<T>(defensiveCopy(source.getStereotypes()),
55-
source.isAlternative(), source.getName(),
55+
source.isAlternative(), source.isReserve(), source.getName(),
5656
defensiveCopy(source.getQualifiers()), defensiveCopy(source.getTypes()), source.getScope());
5757
return attributes;
5858
}

impl/src/main/java/org/jboss/weld/bean/attributes/ImmutableBeanAttributes.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ public class ImmutableBeanAttributes<T> implements BeanAttributes<T> {
3535

3636
private final Set<Class<? extends Annotation>> stereotypes;
3737
private final boolean alternative;
38+
private final boolean reserve;
3839
private final String name;
3940
private final Set<Annotation> qualifiers;
4041
private final Set<Type> types;
4142
private final Class<? extends Annotation> scope;
4243

43-
public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boolean alternative, String name,
44-
Set<Annotation> qualifiers, Set<Type> types,
45-
Class<? extends Annotation> scope) {
44+
public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean reserve,
45+
String name, Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope) {
4646
this.stereotypes = stereotypes;
4747
this.alternative = alternative;
48+
this.reserve = reserve;
4849
this.name = name;
4950
this.qualifiers = qualifiers;
5051
this.types = types;
@@ -55,7 +56,8 @@ public ImmutableBeanAttributes(Set<Class<? extends Annotation>> stereotypes, boo
5556
* Utility constructor used for overriding Bean qualifiers and name for specialization purposes.
5657
*/
5758
public ImmutableBeanAttributes(Set<Annotation> qualifiers, String name, BeanAttributes<T> attributes) {
58-
this(attributes.getStereotypes(), attributes.isAlternative(), name, qualifiers, attributes.getTypes(),
59+
this(attributes.getStereotypes(), attributes.isAlternative(), attributes.isReserve(), name, qualifiers,
60+
attributes.getTypes(),
5961
attributes.getScope());
6062
}
6163

@@ -69,6 +71,11 @@ public boolean isAlternative() {
6971
return alternative;
7072
}
7173

74+
@Override
75+
public boolean isReserve() {
76+
return reserve;
77+
}
78+
7279
@Override
7380
public String getName() {
7481
return name;

impl/src/main/java/org/jboss/weld/bean/builtin/AbstractBuiltInBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public boolean isDependentContextOptimizationAllowed() {
9898
protected static class BuiltInBeanAttributes<T> extends ImmutableBeanAttributes<T> {
9999

100100
public BuiltInBeanAttributes(Class<T> type) {
101-
super(Collections.<Class<? extends Annotation>> emptySet(), false, null, Bindings.DEFAULT_QUALIFIERS,
101+
super(Collections.<Class<? extends Annotation>> emptySet(), false, false, null, Bindings.DEFAULT_QUALIFIERS,
102102
ImmutableSet.of(Object.class, type), Dependent.class);
103103
}
104104
}

impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import jakarta.annotation.Priority;
2626
import jakarta.decorator.Decorator;
27+
import jakarta.enterprise.inject.Reserve;
2728
import jakarta.enterprise.inject.Stereotype;
2829
import jakarta.enterprise.inject.spi.AnnotatedType;
2930
import jakarta.enterprise.inject.spi.Bean;
@@ -167,6 +168,9 @@ private void processPriority(AnnotatedType<?> type) {
167168
globalEnablementBuilder.addInterceptor(type.getJavaClass(), value);
168169
} else if (type.isAnnotationPresent(Decorator.class)) {
169170
globalEnablementBuilder.addDecorator(type.getJavaClass(), value);
171+
// TODO below is probably not enough? Both alternative and reserve can be declared on stereotypes!
172+
} else if (type.isAnnotationPresent(Reserve.class)) {
173+
globalEnablementBuilder.addReserve(type.getJavaClass(), value);
170174
} else {
171175
/*
172176
* An alternative may be given a priority for the application by placing the @Priority annotation on the bean

impl/src/main/java/org/jboss/weld/bootstrap/enablement/EnablementListView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public void remove() {
269269
enum ViewType {
270270

271271
ALTERNATIVES("getAlternatives()"),
272+
RESERVES("getReserves()"),
272273
INTERCEPTORS("getInterceptors()"),
273274
DECORATORS("getDecorators()");
274275

impl/src/main/java/org/jboss/weld/bootstrap/enablement/GlobalEnablementBuilder.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
public class GlobalEnablementBuilder extends AbstractBootstrapService {
5757

5858
private final List<Item> alternatives = Collections.synchronizedList(new ArrayList<Item>());
59+
private final List<Item> reserves = Collections.synchronizedList(new ArrayList<Item>());
5960
private final List<Item> interceptors = Collections.synchronizedList(new ArrayList<Item>());
6061
private final List<Item> decorators = Collections.synchronizedList(new ArrayList<Item>());
6162

6263
private volatile Map<Class<?>, Integer> cachedAlternativeMap;
64+
private volatile Map<Class<?>, Integer> cachedReserveMap;
6365
private volatile boolean sorted;
6466
private volatile boolean dirty;
6567

@@ -83,6 +85,10 @@ public void addAlternative(Class<?> javaClass, int priority) {
8385
addItem(alternatives, javaClass, priority);
8486
}
8587

88+
public void addReserve(Class<?> javaClass, int priority) {
89+
addItem(reserves, javaClass, priority);
90+
}
91+
8692
public void addInterceptor(Class<?> javaClass, int priority) {
8793
addItem(interceptors, javaClass, priority);
8894
}
@@ -112,6 +118,27 @@ protected List<Item> getDelegate() {
112118
};
113119
}
114120

121+
public List<Class<?>> getReserveList(final Extension extension) {
122+
initialize();
123+
return new EnablementListView() {
124+
125+
@Override
126+
protected Extension getExtension() {
127+
return extension;
128+
}
129+
130+
@Override
131+
protected ViewType getViewType() {
132+
return ViewType.ALTERNATIVES;
133+
}
134+
135+
@Override
136+
protected List<Item> getDelegate() {
137+
return reserves;
138+
}
139+
};
140+
}
141+
115142
public List<Class<?>> getInterceptorList(final Extension extension) {
116143
initialize();
117144
return new EnablementListView() {
@@ -180,9 +207,21 @@ private Map<Class<?>, Integer> getGlobalAlternativeMap() {
180207
return cachedAlternativeMap;
181208
}
182209

210+
private Map<Class<?>, Integer> getGlobalReserveMap() {
211+
if (cachedReserveMap == null || dirty) {
212+
Map<Class<?>, Integer> map = new HashMap<Class<?>, Integer>();
213+
for (Item item : reserves) {
214+
map.put(item.getJavaClass(), item.getPriority());
215+
}
216+
cachedReserveMap = ImmutableMap.copyOf(map);
217+
}
218+
return cachedReserveMap;
219+
}
220+
183221
private void initialize() {
184222
if (!sorted) {
185223
Collections.sort(alternatives);
224+
Collections.sort(reserves);
186225
Collections.sort(interceptors);
187226
Collections.sort(decorators);
188227
sorted = true;
@@ -235,27 +274,31 @@ public ModuleEnablement createModuleEnablement(BeanDeployment deployment) {
235274
alternativeStereotypes = Collections.emptySet();
236275
}
237276

277+
// TODO inline these?
238278
Map<Class<?>, Integer> globalAlternatives = getGlobalAlternativeMap();
279+
Map<Class<?>, Integer> globalReserves = getGlobalReserveMap();
239280

240281
// We suppose that enablements are always created all at once
241282
dirty = false;
242283

243284
return new ModuleEnablement(moduleInterceptorsBuilder.build(), moduleDecoratorsBuilder.build(), globalAlternatives,
285+
globalReserves,
244286
alternativeClasses,
245287
alternativeStereotypes);
246288
}
247289

248290
@Override
249291
public void cleanupAfterBoot() {
250292
alternatives.clear();
293+
reserves.clear();
251294
interceptors.clear();
252295
decorators.clear();
253296
}
254297

255298
@Override
256299
public String toString() {
257-
return "GlobalEnablementBuilder [alternatives=" + alternatives + ", interceptors=" + interceptors + ", decorators="
258-
+ decorators + "]";
300+
return "GlobalEnablementBuilder [alternatives=" + alternatives + ", reserves=" + reserves + ", interceptors="
301+
+ interceptors + ", decorators=" + decorators + "]";
259302
}
260303

261304
private <T> void checkForDuplicates(List<Metadata<T>> list, MessageCallback<DeploymentException> messageCallback) {

impl/src/main/java/org/jboss/weld/bootstrap/enablement/ModuleEnablement.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
public class ModuleEnablement {
4343

4444
public static final ModuleEnablement EMPTY_ENABLEMENT = new ModuleEnablement(Collections.<Class<?>> emptyList(),
45-
Collections.<Class<?>> emptyList(), Collections.<Class<?>, Integer> emptyMap(), Collections.<Class<?>> emptySet(),
45+
Collections.<Class<?>> emptyList(), Collections.<Class<?>, Integer> emptyMap(),
46+
Collections.<Class<?>, Integer> emptyMap(), Collections.<Class<?>> emptySet(),
4647
Collections.<Class<? extends Annotation>> emptySet());
4748

4849
private final List<Class<?>> interceptors;
@@ -51,6 +52,7 @@ Collections.<Class<?>> emptyList(), Collections.<Class<?>, Integer> emptyMap(),
5152
private final Map<Class<?>, Integer> interceptorMap;
5253
private final Map<Class<?>, Integer> decoratorMap;
5354
private final Map<Class<?>, Integer> globalAlternatives;
55+
private final Map<Class<?>, Integer> globalReserves;
5456

5557
private final Set<Class<?>> localAlternativeClasses;
5658
private final Set<Class<? extends Annotation>> localAlternativeStereotypes;
@@ -59,7 +61,8 @@ Collections.<Class<?>> emptyList(), Collections.<Class<?>, Integer> emptyMap(),
5961
private final Comparator<Interceptor<?>> interceptorComparator;
6062

6163
public ModuleEnablement(List<Class<?>> interceptors, List<Class<?>> decorators, Map<Class<?>, Integer> globalAlternatives,
62-
Set<Class<?>> localAlternativeClasses, Set<Class<? extends Annotation>> localAlternativeStereotypes) {
64+
Map<Class<?>, Integer> globalReserves, Set<Class<?>> localAlternativeClasses,
65+
Set<Class<? extends Annotation>> localAlternativeStereotypes) {
6366
this.interceptors = interceptors;
6467
this.decorators = decorators;
6568

@@ -70,6 +73,7 @@ public ModuleEnablement(List<Class<?>> interceptors, List<Class<?>> decorators,
7073
this.interceptorComparator = new EnablementComparator<Interceptor<?>>(interceptorMap);
7174

7275
this.globalAlternatives = globalAlternatives;
76+
this.globalReserves = globalReserves;
7377

7478
this.localAlternativeClasses = localAlternativeClasses;
7579
this.localAlternativeStereotypes = localAlternativeStereotypes;
@@ -114,14 +118,27 @@ public Integer getAlternativePriority(Class<?> javaClass) {
114118
return globalAlternatives.get(javaClass);
115119
}
116120

121+
public Integer getReservePriority(Class<?> javaClass) {
122+
return globalReserves.get(javaClass);
123+
}
124+
117125
public boolean isEnabledAlternativeClass(Class<?> alternativeClass) {
118126
return globalAlternatives.containsKey(alternativeClass) || localAlternativeClasses.contains(alternativeClass);
119127
}
120128

129+
public boolean isEnabledReserveClass(Class<?> reserveClass) {
130+
return globalReserves.containsKey(reserveClass);
131+
}
132+
121133
public boolean isEnabledAlternativeStereotype(Class<?> alternativeClass) {
122134
return globalAlternatives.containsKey(alternativeClass) || localAlternativeStereotypes.contains(alternativeClass);
123135
}
124136

137+
// TODO this seems awkward, why would I check the same classes as with #isEnabledReserveClass?
138+
public boolean isEnabledReserveStereotype(Class<?> reserveClass) {
139+
return globalReserves.containsKey(reserveClass);
140+
}
141+
125142
public Set<Class<?>> getAlternativeClasses() {
126143
return localAlternativeClasses;
127144
}

impl/src/main/java/org/jboss/weld/bootstrap/events/AfterBeanDiscoveryImpl.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,12 @@ private <T> void processBeanRegistration(BeanRegistration registration, GlobalEn
232232
}
233233
} else {
234234
beanManager.addBean(bean);
235-
if (priority != null && bean.isAlternative()) {
236-
globalEnablementBuilder.addAlternative(bean.getBeanClass(), priority);
235+
if (priority != null) {
236+
if (bean.isAlternative()) {
237+
globalEnablementBuilder.addAlternative(bean.getBeanClass(), priority);
238+
} else if (bean.isReserve()) {
239+
globalEnablementBuilder.addReserve(bean.getBeanClass(), priority);
240+
}
237241
}
238242
}
239243
containerLifecycleEvents.fireProcessBean(beanManager, bean, registration.extension);

impl/src/main/java/org/jboss/weld/bootstrap/events/AfterTypeDiscoveryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public List<Class<?>> getAlternatives() {
7070

7171
@Override
7272
public List<Class<?>> getReserves() {
73-
throw new UnsupportedOperationException("Not yet implemented");
73+
checkWithinObserverNotification();
74+
return builder.getReserveList(getReceiver());
7475

7576
}
7677

0 commit comments

Comments
 (0)