Skip to content

Commit

Permalink
WELD-2768 support explicitly declaring @priority on producers
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Dec 1, 2023
1 parent 8c1438f commit ab065c5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.lang.reflect.Type;
import java.util.Set;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.AnnotatedMember;
Expand Down Expand Up @@ -59,6 +60,7 @@ public abstract class AbstractProducerBean<X, T, S extends Member> extends Abstr
// Passivation flags
private boolean passivationCapableBean;
private boolean passivationCapableDependency;
protected Integer explicitPriority;

/**
* Constructor
Expand All @@ -70,6 +72,8 @@ public AbstractProducerBean(BeanAttributes<T> attributes, BeanIdentifier identif
BeanManagerImpl beanManager, ServiceRegistry services) {
super(attributes, identifier, beanManager);
this.declaringBean = declaringBean;
Priority annotation = getAnnotated() == null ? null : getAnnotated().getAnnotation(Priority.class);
this.explicitPriority = annotation == null ? null : annotation.value();
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions impl/src/main/java/org/jboss/weld/bean/ProducerField.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ public boolean isProxyable() {
public Set<AbstractBean<?, ?>> getSpecializedBeans() {
throw new UnsupportedOperationException("Producer field may not specialize other beans " + this);
}

@Override
public Integer getPriority() {
return explicitPriority;
}
}
5 changes: 5 additions & 0 deletions impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,9 @@ public String toString() {
public boolean isProxyable() {
return proxiable;
}

@Override
public Integer getPriority() {
return explicitPriority;
}
}
6 changes: 4 additions & 2 deletions impl/src/main/java/org/jboss/weld/bean/WeldBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ public interface WeldBean<T> extends Bean<T> {
BeanIdentifier getIdentifier();

/**
* Used for custom beans registered via WeldBeanConfigurator.
* Used for custom beans registered via WeldBeanConfigurator and for {@link ProducerField} and {@link ProducerMethod}
* if they explicitly declare the {@link jakarta.annotation.Priority} annotation.
* All other implementations will return {@code null} by default.
*
* @return bean priority or null if not set or overriden
* @return bean priority or null if not set or overridden
*/
default Integer getPriority() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,18 @@ public Set<Bean<?>> apply(Set<Bean<?>> from) {
public Set<Bean<?>> resolveAlternatives(Set<Bean<?>> alternatives) {
int highestPriority = Integer.MIN_VALUE;
Set<Bean<?>> selectedAlternativesWithHighestPriority = new HashSet<Bean<?>>();

for (Bean<?> bean : alternatives) {
Integer priority = beanManager.getEnabled().getAlternativePriority(bean.getBeanClass());
Integer priority;
if (bean instanceof AbstractProducerBean) {
// first check for explicit priority declaration on producers
priority = ((AbstractProducerBean<?, ?, ?>) bean).getPriority();
// if not found, fall back to priority on declaring bean
if (priority == null) {
priority = beanManager.getEnabled().getAlternativePriority(bean.getBeanClass());
}
} else {
priority = beanManager.getEnabled().getAlternativePriority(bean.getBeanClass());
}
if (priority == null) {
// not all the beans left are alternatives with a priority - we are not able to resolve
return ImmutableSet.copyOf(alternatives);
Expand Down

0 comments on commit ab065c5

Please sign in to comment.