diff --git a/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java b/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java index c3ea3e0a..8234afb4 100644 --- a/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java +++ b/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java @@ -73,6 +73,7 @@ static void scanForRequiredBeanClasses(List> testClasses, Weld weld, bo Set> foundClasses = new HashSet<>(); Set excludedBeanTypes = new HashSet<>(); Set> excludedBeanClasses = new HashSet<>(); + boolean syntheticArchiveDiscoverySet = false; while (!classesToProcess.isEmpty()) { @@ -198,6 +199,15 @@ static void scanForRequiredBeanClasses(List> testClasses, Weld weld, bo .distinct() .forEach(excludedBeanClasses::add); + // discovery mode can only be set once; we use the first annotation we find + if (!syntheticArchiveDiscoverySet) { + Optional annotation = AnnotationSupport.findAnnotation(currClass, SetSyntheticArchiveDiscoveryMode.class); + if (annotation.isPresent()) { + syntheticArchiveDiscoverySet = true; + weld.setBeanDiscoveryMode(annotation.get().value()); + } + } + } for (Class foundClass : foundClasses) { diff --git a/junit5/src/main/java/org/jboss/weld/junit5/auto/SetSyntheticArchiveDiscoveryMode.java b/junit5/src/main/java/org/jboss/weld/junit5/auto/SetSyntheticArchiveDiscoveryMode.java new file mode 100644 index 00000000..bf16eaaa --- /dev/null +++ b/junit5/src/main/java/org/jboss/weld/junit5/auto/SetSyntheticArchiveDiscoveryMode.java @@ -0,0 +1,26 @@ +package org.jboss.weld.junit5.auto; + +import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Sets discovery mode for Weld SE synthetic bean archive. Valid options are {@link BeanDiscoveryMode#ALL} and + * {@link BeanDiscoveryMode#ANNOTATED}. + *

+ * Starting with Weld 5 (CDI 4), the default value is {@code ANNOTATED}. Applications can leverage this annotation to + * enforce same behavior as older Weld versions where synthetic archives used discovery mode {@code ALL}. + *

+ * This annotation is equal to invocation of {@link org.jboss.weld.environment.se.Weld#setBeanDiscoveryMode(BeanDiscoveryMode)}. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface SetSyntheticArchiveDiscoveryMode { + + BeanDiscoveryMode value(); +} diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/SetDiscoveryModeAllTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/SetDiscoveryModeAllTest.java new file mode 100644 index 00000000..9373e1ff --- /dev/null +++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/SetDiscoveryModeAllTest.java @@ -0,0 +1,31 @@ +package org.jboss.weld.junit5.auto; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import jakarta.enterprise.inject.Instance; +import jakarta.inject.Inject; +import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode; +import org.jboss.weld.junit5.auto.discovery.WithBeanDefiningAnnotation; +import org.jboss.weld.junit5.auto.discovery.WithoutBeanDefiningAnnotation; +import org.junit.jupiter.api.Test; + +@EnableAutoWeld +@AddPackages(value = WithBeanDefiningAnnotation.class, recursively = false) +@SetSyntheticArchiveDiscoveryMode(BeanDiscoveryMode.ALL) +public class SetDiscoveryModeAllTest { + + @Inject + private WithBeanDefiningAnnotation standardBean; + + @Inject + Instance instance; + + @Test + void testDiscoveryModeWasChanged() { + // bean with bean defining annotation is normally resolvable + assertNotNull(standardBean); + // bean without bean defining annotation is discovered as well because we set discovery to ALL + assertTrue(instance.select(WithoutBeanDefiningAnnotation.class).isResolvable()); + } +} diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/discovery/WithBeanDefiningAnnotation.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/discovery/WithBeanDefiningAnnotation.java new file mode 100644 index 00000000..17123076 --- /dev/null +++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/discovery/WithBeanDefiningAnnotation.java @@ -0,0 +1,7 @@ +package org.jboss.weld.junit5.auto.discovery; + +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class WithBeanDefiningAnnotation { +} diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/discovery/WithoutBeanDefiningAnnotation.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/discovery/WithoutBeanDefiningAnnotation.java new file mode 100644 index 00000000..aefada28 --- /dev/null +++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/discovery/WithoutBeanDefiningAnnotation.java @@ -0,0 +1,5 @@ +package org.jboss.weld.junit5.auto.discovery; + +// intentionally doesn't have any bean defining annotation +public class WithoutBeanDefiningAnnotation { +}