Skip to content

Commit

Permalink
ArC - fix behavior of List injection with io.quarkus.arc.All qualifier
Browse files Browse the repository at this point in the history
- if no other qualifier is declared then @Any is used, i.e. the behavior
should be equivalent to `@Inject @Any Instance<>`
  • Loading branch information
mkouba committed Sep 19, 2022
1 parent b6f6718 commit 492b29c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ interface Converter {

}

@MyQualifier
@Singleton
static class ServiceAlpha implements Service {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -46,11 +47,19 @@ public static List<InjectableBean<?>> resolveBeans(Type requiredType, Annotation
return List.copyOf(nonSuppressed);
}

private static List<InjectableBean<?>> resolveAllBeans(Type requiredType, Set<Annotation> 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 <T> List<T> listOf(InjectableBean<?> targetBean, Type injectionPointType, Type requiredType,
Set<Annotation> requiredQualifiers,
CreationalContextImpl<?> creationalContext, Set<Annotation> annotations, Member javaMember, int position) {
List<InjectableBean<?>> beans = resolveBeans(requiredType, requiredQualifiers);
List<InjectableBean<?>> beans = resolveAllBeans(requiredType, requiredQualifiers);
if (beans.isEmpty()) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -87,7 +96,7 @@ public InjectionPoint get() {
public static <T> List<InstanceHandle<T>> listOfHandles(Supplier<InjectionPoint> injectionPoint, Type requiredType,
Set<Annotation> requiredQualifiers,
CreationalContextImpl<?> creationalContext) {
List<InjectableBean<?>> beans = resolveBeans(requiredType, requiredQualifiers);
List<InjectableBean<?>> beans = resolveAllBeans(requiredType, requiredQualifiers);
if (beans.isEmpty()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,7 @@ public class ListAllTest {

@Test
public void testSelectAll() {
// the behavior should be equivalent to @Inject @Any Instance<Service>
List<InstanceHandle<Service>> services = Arc.container().listAll(Service.class);
assertEquals(2, services.size());
assertThatExceptionOfType(UnsupportedOperationException.class)
Expand Down Expand Up @@ -63,6 +65,7 @@ public String ping() {
}
}

@MyQualifier
@Priority(5) // this impl should go first
@Dependent
static class ServiceBravo implements Service {
Expand Down

0 comments on commit 492b29c

Please sign in to comment.