From 492b29c0636c443d41a841cdd9669b2d245ae570 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Mon, 19 Sep 2022 14:24:14 +0200 Subject: [PATCH] ArC - fix behavior of List injection with io.quarkus.arc.All qualifier - if no other qualifier is declared then @Any is used, i.e. the behavior should be equivalent to `@Inject @Any Instance<>` --- .../quarkus/arc/test/lookup/ListInjectionTest.java | 1 + .../main/java/io/quarkus/arc/impl/Instances.java | 13 +++++++++++-- .../java/io/quarkus/arc/test/all/ListAllTest.java | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/lookup/ListInjectionTest.java b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/lookup/ListInjectionTest.java index f2315206a5e79..5803d5a16ce73 100644 --- a/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/lookup/ListInjectionTest.java +++ b/extensions/arc/deployment/src/test/java/io/quarkus/arc/test/lookup/ListInjectionTest.java @@ -143,6 +143,7 @@ interface Converter { } + @MyQualifier @Singleton static class ServiceAlpha implements Service { diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Instances.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Instances.java index 6e75c4e3ed288..6bfb25154a00a 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Instances.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/Instances.java @@ -13,6 +13,7 @@ import java.util.Set; import java.util.function.Supplier; import javax.enterprise.context.Dependent; +import javax.enterprise.inject.Any; import javax.enterprise.inject.spi.InjectionPoint; public final class Instances { @@ -46,11 +47,19 @@ public static List> resolveBeans(Type requiredType, Annotation return List.copyOf(nonSuppressed); } + private static List> resolveAllBeans(Type requiredType, Set requiredQualifiers) { + if (requiredQualifiers == null || requiredQualifiers.isEmpty()) { + // If no qualifier is specified then @Any is used + return resolveBeans(requiredType, new Annotation[] { Any.Literal.INSTANCE }); + } + return resolveBeans(requiredType, requiredQualifiers.toArray(EMPTY_ANNOTATION_ARRAY)); + } + @SuppressWarnings("unchecked") public static List listOf(InjectableBean targetBean, Type injectionPointType, Type requiredType, Set requiredQualifiers, CreationalContextImpl creationalContext, Set annotations, Member javaMember, int position) { - List> beans = resolveBeans(requiredType, requiredQualifiers); + List> beans = resolveAllBeans(requiredType, requiredQualifiers); if (beans.isEmpty()) { return Collections.emptyList(); } @@ -87,7 +96,7 @@ public InjectionPoint get() { public static List> listOfHandles(Supplier injectionPoint, Type requiredType, Set requiredQualifiers, CreationalContextImpl creationalContext) { - List> beans = resolveBeans(requiredType, requiredQualifiers); + List> beans = resolveAllBeans(requiredType, requiredQualifiers); if (beans.isEmpty()) { return Collections.emptyList(); } diff --git a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/all/ListAllTest.java b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/all/ListAllTest.java index a1578c6bcf307..41bc7d80a6500 100644 --- a/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/all/ListAllTest.java +++ b/independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/all/ListAllTest.java @@ -7,6 +7,7 @@ import io.quarkus.arc.Arc; import io.quarkus.arc.InstanceHandle; import io.quarkus.arc.test.ArcTestContainer; +import io.quarkus.arc.test.MyQualifier; import java.util.List; import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; @@ -26,6 +27,7 @@ public class ListAllTest { @Test public void testSelectAll() { + // the behavior should be equivalent to @Inject @Any Instance List> services = Arc.container().listAll(Service.class); assertEquals(2, services.size()); assertThatExceptionOfType(UnsupportedOperationException.class) @@ -63,6 +65,7 @@ public String ping() { } } + @MyQualifier @Priority(5) // this impl should go first @Dependent static class ServiceBravo implements Service {