Skip to content

Commit 7b134ce

Browse files
committed
ArcContainer#instance(): use minimal injection point instead of EMPTY
1 parent a8c5123 commit 7b134ce

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,18 +338,18 @@ public <T> Supplier<InstanceHandle<T>> beanInstanceSupplier(Class<T> type, Annot
338338
if (bean == null) {
339339
return null;
340340
}
341+
InjectionPoint injectionPoint = InjectionPointImpl.of(type, qualifiers);
341342
return new Supplier<InstanceHandle<T>>() {
342343
@Override
343344
public InstanceHandle<T> get() {
344-
return beanInstanceHandle(bean, null);
345+
return beanInstanceHandle(bean, null, injectionPoint, null);
345346
}
346347
};
347348
}
348349

349350
@Override
350351
public <T> InstanceHandle<T> instance(InjectableBean<T> bean) {
351-
Objects.requireNonNull(bean);
352-
return beanInstanceHandle(bean, null);
352+
return beanInstanceHandle(Objects.requireNonNull(bean), null, InjectionPointImpl.EMPTY, null);
353353
}
354354

355355
@Override
@@ -404,7 +404,7 @@ public <T> InstanceHandle<T> instance(String name) {
404404
Set<InjectableBean<?>> resolvedBeans = beansByName.getValue(name);
405405
return resolvedBeans.size() != 1 ? EagerInstanceHandle.unavailable()
406406
: (InstanceHandle<T>) beanInstanceHandle(resolvedBeans.iterator()
407-
.next(), null);
407+
.next(), null, InjectionPointImpl.EMPTY, null);
408408
}
409409

410410
@Override
@@ -552,16 +552,21 @@ private static void addBuiltInBeans(List<InjectableBean<?>> beans, Map<String, L
552552
}
553553

554554
private <T> InstanceHandle<T> instanceHandle(Type type, Annotation... qualifiers) {
555-
return beanInstanceHandle(getBean(type, qualifiers), null);
555+
if (qualifiers == null || qualifiers.length == 0) {
556+
qualifiers = new Annotation[] { Default.Literal.INSTANCE };
557+
} else {
558+
registeredQualifiers.verify(qualifiers);
559+
}
560+
return beanInstanceHandle(getBean(type, qualifiers), null, InjectionPointImpl.of(type, qualifiers), null);
556561
}
557562

558563
static <T> InstanceHandle<T> beanInstanceHandle(InjectableBean<T> bean, CreationalContextImpl<T> parentContext,
559-
boolean resetCurrentInjectionPoint, Consumer<T> destroyLogic) {
560-
return beanInstanceHandle(bean, parentContext, resetCurrentInjectionPoint, destroyLogic, false);
564+
InjectionPoint resetInjectionPoint, Consumer<T> destroyLogic) {
565+
return beanInstanceHandle(bean, parentContext, resetInjectionPoint, destroyLogic, false);
561566
}
562567

563568
static <T> InstanceHandle<T> beanInstanceHandle(InjectableBean<T> bean, CreationalContextImpl<T> parentContext,
564-
boolean resetCurrentInjectionPoint, Consumer<T> destroyLogic, boolean useParentCreationalContextDirectly) {
569+
InjectionPoint resetInjectionPoint, Consumer<T> destroyLogic, boolean useParentCreationalContextDirectly) {
565570
if (bean != null) {
566571
if (parentContext == null && Dependent.class.equals(bean.getScope())) {
567572
parentContext = new CreationalContextImpl<>(null);
@@ -573,14 +578,14 @@ static <T> InstanceHandle<T> beanInstanceHandle(InjectableBean<T> bean, Creation
573578
creationalContext = new CreationalContextImpl<>(bean);
574579
}
575580
InjectionPoint prev = null;
576-
if (resetCurrentInjectionPoint) {
577-
prev = InjectionPointProvider.setCurrent(creationalContext, CurrentInjectionPointProvider.EMPTY);
581+
if (resetInjectionPoint != null) {
582+
prev = InjectionPointProvider.setCurrent(creationalContext, resetInjectionPoint);
578583
}
579584
try {
580585
return new EagerInstanceHandle<>(bean, bean.get(creationalContext), creationalContext, parentContext,
581586
destroyLogic);
582587
} finally {
583-
if (resetCurrentInjectionPoint) {
588+
if (resetInjectionPoint != null) {
584589
InjectionPointProvider.setCurrent(creationalContext, prev);
585590
}
586591
}
@@ -589,17 +594,8 @@ static <T> InstanceHandle<T> beanInstanceHandle(InjectableBean<T> bean, Creation
589594
}
590595
}
591596

592-
static <T> InstanceHandle<T> beanInstanceHandle(InjectableBean<T> bean, CreationalContextImpl<T> parentContext) {
593-
return beanInstanceHandle(bean, parentContext, true, null);
594-
}
595-
596597
@SuppressWarnings("unchecked")
597598
private <T> InjectableBean<T> getBean(Type requiredType, Annotation... qualifiers) {
598-
if (qualifiers == null || qualifiers.length == 0) {
599-
qualifiers = new Annotation[] { Default.Literal.INSTANCE };
600-
} else {
601-
registeredQualifiers.verify(qualifiers);
602-
}
603599
Resolvable resolvable = new Resolvable(requiredType, qualifiers);
604600
Set<InjectableBean<?>> resolvedBeans = resolved.getValue(resolvable);
605601
if (resolvedBeans.isEmpty()) {
@@ -1055,7 +1051,7 @@ public static ArcContainerImpl instance() {
10551051
<T> EventImpl<T> getEvent(Type eventType, Set<Annotation> eventQualifiers, InjectionPoint ip) {
10561052
if (eventMocks != null) {
10571053
AtomicReference<Event<?>> mock = eventMocks.computeIfAbsent(
1058-
new TypeAndQualifiers(eventType, eventQualifiers),
1054+
new TypeAndQualifiers(ip.getType(), ip.getQualifiers()),
10591055
ArcContainerImpl::newEventMockReference);
10601056
return new MockableEventImpl<>(eventType, eventQualifiers, ip, mock);
10611057
} else {

independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/BeanManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> ctx
6969
InjectionPoint prev = InjectionPointProvider.setCurrent(ctx, null);
7070
try {
7171
return ArcContainerImpl.beanInstanceHandle((InjectableBean) bean, (CreationalContextImpl) ctx,
72-
false, null, true).get();
72+
null, null, true).get();
7373
} finally {
7474
InjectionPointProvider.setCurrent(ctx, prev);
7575
}
@@ -93,7 +93,7 @@ public Object getInjectableReference(InjectionPoint ij, CreationalContext<?> ctx
9393
InjectionPoint prev = InjectionPointProvider.setCurrent(ctx, ij);
9494
try {
9595
return ArcContainerImpl.beanInstanceHandle(bean, (CreationalContextImpl) ctx,
96-
false, null, true).get();
96+
null, null, true).get();
9797
} finally {
9898
InjectionPointProvider.setCurrent(ctx, prev);
9999
}

independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/CurrentInjectionPointProvider.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.lang.annotation.Annotation;
44
import java.lang.reflect.Member;
55
import java.lang.reflect.Type;
6-
import java.util.Collections;
76
import java.util.Set;
87
import java.util.function.Supplier;
98

@@ -18,14 +17,11 @@
1817
*/
1918
public class CurrentInjectionPointProvider<T> implements InjectableReferenceProvider<T> {
2019

21-
static final InjectionPoint EMPTY = new InjectionPointImpl(Object.class, Object.class, Collections.emptySet(), null, null,
22-
null, -1, false);
23-
2420
static final Supplier<InjectionPoint> EMPTY_SUPPLIER = new Supplier<InjectionPoint>() {
2521

2622
@Override
2723
public InjectionPoint get() {
28-
return CurrentInjectionPointProvider.EMPTY;
24+
return InjectionPointImpl.EMPTY;
2925
}
3026
};
3127

independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/InjectionPointImpl.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.lang.reflect.Method;
99
import java.lang.reflect.Modifier;
1010
import java.lang.reflect.Type;
11+
import java.util.Collections;
1112
import java.util.HashSet;
1213
import java.util.List;
1314
import java.util.Set;
@@ -26,6 +27,18 @@
2627

2728
public class InjectionPointImpl implements InjectionPoint {
2829

30+
static final InjectionPoint EMPTY = new InjectionPointImpl(Object.class, Object.class, Collections.emptySet(), null, null,
31+
null, -1, false);
32+
33+
/**
34+
* @param requiredType
35+
* @param qualifiers
36+
* @return a new injection point with the given required type and qualifiers
37+
*/
38+
public static InjectionPointImpl of(Type requiredType, Annotation... qualifiers) {
39+
return new InjectionPointImpl(null, requiredType, Set.of(qualifiers), null, Set.of(), null, 0, false);
40+
}
41+
2942
private final Type requiredType;
3043
private final Set<Annotation> qualifiers;
3144
private final InjectableBean<?> bean;
@@ -37,7 +50,7 @@ public InjectionPointImpl(Type injectionPointType, Type requiredType, Set<Annota
3750
InjectableBean<?> bean, Set<Annotation> annotations, Member javaMember,
3851
int position, boolean isTransient) {
3952
this.requiredType = requiredType;
40-
this.qualifiers = CollectionHelpers.toImmutableSmallSet(qualifiers);
53+
this.qualifiers = qualifiers != null ? Set.copyOf(qualifiers) : null;
4154
this.bean = bean;
4255
if (javaMember instanceof Executable) {
4356
this.annotated = new InjectionPointImpl.AnnotatedParameterImpl<>(injectionPointType, annotations, position,

independent-projects/arc/tests/src/test/java/io/quarkus/arc/test/injectionpoint/InjectionPointMetadataWithDynamicLookupTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ public class InjectionPointMetadataWithDynamicLookupTest {
3232
@Test
3333
public void arcContainerInstance() {
3434
// the "current" injection point of `Arc.container().instance(...)` doesn't seem to be well defined
35+
// but the injection point used does support the required type and qualifiers
3536
BeanWithInjectionPointMetadata bean = Arc.container().instance(BeanWithInjectionPointMetadata.class).get();
3637

3738
// this is probably an implementation artifact, not an intentional choice
3839
bean.assertPresent(ip -> {
39-
assertEquals(Object.class, ip.getType());
40-
assertEquals(Set.of(), ip.getQualifiers());
40+
assertEquals(BeanWithInjectionPointMetadata.class, ip.getType());
41+
assertEquals(1, ip.getQualifiers().size()); // @Default
4142
assertNull(ip.getMember());
4243
assertNull(ip.getBean());
4344
});

0 commit comments

Comments
 (0)