From 42cc27170c2293096dfbf12e369b2cea263c9ae1 Mon Sep 17 00:00:00 2001 From: Matej Novotny Date: Thu, 30 Nov 2023 18:55:21 +0100 Subject: [PATCH] WELD-2768 support explicitly declaring @Priority on producers --- .../org/jboss/weld/bean/AbstractProducerBean.java | 9 +++++++++ .../src/main/java/org/jboss/weld/bean/WeldBean.java | 6 ++++-- .../resolution/AbstractTypeSafeBeanResolver.java | 13 +++++++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java b/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java index ee180376d2..14d73a3135 100644 --- a/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java +++ b/impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java @@ -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; @@ -59,6 +60,7 @@ public abstract class AbstractProducerBean extends Abstr // Passivation flags private boolean passivationCapableBean; private boolean passivationCapableDependency; + protected Integer explicitPriority; /** * Constructor @@ -70,6 +72,8 @@ public AbstractProducerBean(BeanAttributes 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 @@ -217,4 +221,9 @@ public AbstractClassBean getDeclaringBean() { @Override public abstract EnhancedAnnotatedMember getEnhancedAnnotated(); + + @Override + public Integer getPriority() { + return explicitPriority; + } } diff --git a/impl/src/main/java/org/jboss/weld/bean/WeldBean.java b/impl/src/main/java/org/jboss/weld/bean/WeldBean.java index 3291deb1af..fbaadbf394 100644 --- a/impl/src/main/java/org/jboss/weld/bean/WeldBean.java +++ b/impl/src/main/java/org/jboss/weld/bean/WeldBean.java @@ -35,9 +35,11 @@ public interface WeldBean extends Bean { 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; diff --git a/impl/src/main/java/org/jboss/weld/resolution/AbstractTypeSafeBeanResolver.java b/impl/src/main/java/org/jboss/weld/resolution/AbstractTypeSafeBeanResolver.java index 49c21466e1..02cd62d9b4 100644 --- a/impl/src/main/java/org/jboss/weld/resolution/AbstractTypeSafeBeanResolver.java +++ b/impl/src/main/java/org/jboss/weld/resolution/AbstractTypeSafeBeanResolver.java @@ -110,9 +110,18 @@ public Set> apply(Set> from) { public Set> resolveAlternatives(Set> alternatives) { int highestPriority = Integer.MIN_VALUE; Set> selectedAlternativesWithHighestPriority = new HashSet>(); - 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);