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 5, 2023
1 parent 0fe03ee commit 5c8f377
Show file tree
Hide file tree
Showing 23 changed files with 527 additions and 4 deletions.
13 changes: 13 additions & 0 deletions impl/src/main/java/org/jboss/weld/bean/AbstractProducerBean.java
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 Down Expand Up @@ -217,4 +219,15 @@ public AbstractClassBean<X> getDeclaringBean() {

@Override
public abstract EnhancedAnnotatedMember<T, ?, S> getEnhancedAnnotated();

@Override
public Integer getPriority() {
return explicitPriority;
}

// requires initialized getAnnotated() hence subclasses need to invoke this manually
protected void processExplicitPriority() {
Priority annotation = getAnnotated() == null ? null : getAnnotated().getAnnotation(Priority.class);
this.explicitPriority = annotation == null ? null : annotation.value();
}
}
1 change: 1 addition & 0 deletions impl/src/main/java/org/jboss/weld/bean/ProducerField.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public Bean<T> getBean() {
return ProducerField.this;
}
});
processExplicitPriority();
}

@Override
Expand Down
1 change: 1 addition & 0 deletions impl/src/main/java/org/jboss/weld/bean/ProducerMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public Bean<T> getBean() {
return ProducerMethod.this;
}
});
processExplicitPriority();
}

@Override
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jboss.weld.tests.producer.alternative.priority;

public class Alpha {

private String s;

public Alpha(String s) {
this.s = s;
}

public String ping() {
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jboss.weld.tests.producer.alternative.priority;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Produces;

@Alternative
@Priority(1)
@ApplicationScoped
public class AltBeanProducingAlternative {

@Alternative
@Priority(20) // should override class-level priority value and hence end up having the highest priority
@Produces
@ProducedByMethod
Beta producer1() {
return new Beta(ProducerExplicitPriorityTest.ALT2);
}

@Alternative
@Priority(20) // should override class-level priority value and hence end up having the highest priority
@Produces
@ProducedByField
Beta producer2 = new Beta(ProducerExplicitPriorityTest.ALT2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.jboss.weld.tests.producer.alternative.priority;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Produces;

@ApplicationScoped
@Alternative
@Priority(1)
public class AltBeanProducingPrioritizedNonAlternative {

@Priority(20) // should override class-level priority value and hence end up having the highest priority
@Produces
@ProducedByMethod
Delta producer1() {
return new Delta(ProducerExplicitPriorityTest.ALT2);
}

@Priority(20) // should override class-level priority value and hence end up having the highest priority
@Produces
@ProducedByField
Delta producer2 = new Delta(ProducerExplicitPriorityTest.ALT2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jboss.weld.tests.producer.alternative.priority;

public class Beta {

private String s;

public Beta(String s) {
this.s = s;
}

public String ping() {
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jboss.weld.tests.producer.alternative.priority;

public class Delta {

private String s;

public Delta(String s) {
this.s = s;
}

public String ping() {
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jboss.weld.tests.producer.alternative.priority;

public class Gamma {

private String s;

public Gamma(String s) {
this.s = s;
}

public String ping() {
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.jboss.weld.tests.producer.alternative.priority;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Produces;

@ApplicationScoped
public class NonAltBeanProducingAlternative {

@Alternative
@Priority(10)
@Produces
@ProducedByMethod
Alpha producer1() {
return new Alpha(ProducerExplicitPriorityTest.ALT);
}

@Alternative
@Priority(10)
@Produces
@ProducedByMethod
Beta producer2() {
return new Beta(ProducerExplicitPriorityTest.ALT);
}

@Alternative
@Priority(10)
@Produces
@ProducedByField
Alpha producer3 = new Alpha(ProducerExplicitPriorityTest.ALT);

@Alternative
@Priority(10)
@Produces
@ProducedByField
Beta producer4 = new Beta(ProducerExplicitPriorityTest.ALT);

@Produces
@ProducedByMethod
@Alternative
@Priority(10)
Gamma producer5() {
return new Gamma(ProducerExplicitPriorityTest.ALT);
}

@Produces
@ProducedByField
@Alternative
@Priority(10)
Gamma producer6 = new Gamma(ProducerExplicitPriorityTest.ALT);

@Produces
@ProducedByMethod
@Alternative
@Priority(10)
Delta producer7() {
return new Delta(ProducerExplicitPriorityTest.ALT);
}

@Produces
@ProducedByField
@Alternative
@Priority(10)
Delta producer8 = new Delta(ProducerExplicitPriorityTest.ALT);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jboss.weld.tests.producer.alternative.priority;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Alternative;
import jakarta.enterprise.inject.Produces;

@ApplicationScoped
@Priority(500)
public class NonAltBeanWithPrioProducingAlternative {

@Produces
@ProducedByMethod
@Alternative
Gamma producer5() {
return new Gamma(ProducerExplicitPriorityTest.ALT2);
}

@Produces
@ProducedByField
@Alternative
Gamma producer6 = new Gamma(ProducerExplicitPriorityTest.ALT2);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.weld.tests.producer.alternative.priority;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ProducedByField {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.weld.tests.producer.alternative.priority;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ProducedByMethod {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.jboss.weld.tests.producer.alternative.priority;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import jakarta.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.test.util.Utils;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class ProducerExplicitPriorityTest {

@Deployment
public static Archive<?> getDeployment() {
return ShrinkWrap.create(BeanArchive.class, Utils.getDeploymentNameAsHash(ProducerExplicitPriorityTest.class))
.addPackage(ProducerExplicitPriorityTest.class.getPackage());
}

public static final String DEFAULT = "default";
public static final String ALT = "alternative";
public static final String ALT2 = "alternative2";

@Inject
@ProducedByMethod
Alpha alphaMethodProducer;

@Inject
@ProducedByField
Alpha alphaFieldProducer;

@Inject
@ProducedByMethod
Beta betaMethodProducer;

@Inject
@ProducedByField
Beta betaFieldProducer;

@Inject
@ProducedByMethod
Gamma gammaMethodProducer;

@Inject
@ProducedByField
Gamma gammaFieldProducer;

@Inject
@ProducedByMethod
Delta deltaMethodProducer;

@Inject
@ProducedByField
Delta deltaFieldProducer;

@Test
public void testAlternativeProducerWithPriority() {
assertNotNull(alphaMethodProducer);
assertNotNull(alphaFieldProducer);

assertEquals(ALT, alphaMethodProducer.ping());
assertEquals(ALT, alphaFieldProducer.ping());
}

@Test
public void testPriorityOnProducerOverPriorityOnClass() {
assertNotNull(betaMethodProducer);
assertNotNull(betaFieldProducer);
assertNotNull(gammaFieldProducer);
assertNotNull(gammaMethodProducer);
assertNotNull(deltaFieldProducer);
assertNotNull(deltaMethodProducer);

assertEquals(ALT2, betaMethodProducer.ping());
assertEquals(ALT2, betaFieldProducer.ping());
assertEquals(ALT2, gammaFieldProducer.ping());
assertEquals(ALT2, gammaMethodProducer.ping());
assertEquals(ALT2, deltaFieldProducer.ping());
assertEquals(ALT2, deltaMethodProducer.ping());
}
}
Loading

0 comments on commit 5c8f377

Please sign in to comment.