From ab065c57d261a05b931b4ac8438836f037093d4d 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 | 4 ++++ .../java/org/jboss/weld/bean/ProducerField.java | 5 +++++ .../java/org/jboss/weld/bean/ProducerMethod.java | 5 +++++ .../src/main/java/org/jboss/weld/bean/WeldBean.java | 6 ++++-- .../resolution/AbstractTypeSafeBeanResolver.java | 13 +++++++++++-- 5 files changed, 29 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..39d5b70d41 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 diff --git a/impl/src/main/java/org/jboss/weld/bean/ProducerField.java b/impl/src/main/java/org/jboss/weld/bean/ProducerField.java index 2cb7a7a450..77be972f06 100644 --- a/impl/src/main/java/org/jboss/weld/bean/ProducerField.java +++ b/impl/src/main/java/org/jboss/weld/bean/ProducerField.java @@ -138,4 +138,9 @@ public boolean isProxyable() { public Set> getSpecializedBeans() { throw new UnsupportedOperationException("Producer field may not specialize other beans " + this); } + + @Override + public Integer getPriority() { + return explicitPriority; + } } diff --git a/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java b/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java index 0f3103a881..7dfd26eaa2 100644 --- a/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java +++ b/impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java @@ -138,4 +138,9 @@ public String toString() { public boolean isProxyable() { return proxiable; } + + @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);