From 4f2aa80a0866ce827bcf114ce5fccb6dcf4379e5 Mon Sep 17 00:00:00 2001
From: Matej Novotny
diff --git a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java index 8f93f767bcadb..51b2cf3627224 100644 --- a/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java +++ b/extensions/arc/runtime/src/main/java/io/quarkus/arc/runtime/BeanContainerImpl.java @@ -21,24 +21,36 @@ class BeanContainerImpl implements BeanContainer { this.container = container; } + @Override + publicT beanInstance(Class beanType, Annotation... beanQualifiers) { + return container.select(beanType, beanQualifiers).get(); + } + @Override public Factory beanInstanceFactory(Class type, Annotation... qualifiers) { Supplier > handleSupplier = container.beanInstanceSupplier(type, qualifiers); - return createFactory(handleSupplier, type, qualifiers); + return createFactory(handleSupplier, null, type, qualifiers); } @Override - public Factory instanceFactory(Class type, Annotation... qualifiers) { - Supplier > handleSupplier = container.instanceSupplier(type, qualifiers); - return createFactory(handleSupplier, type, qualifiers); + public Factory beanInstanceFactory(Supplier > fallbackSupplier, Class type, + Annotation... qualifiers) { + Supplier > handleSupplier = container.beanInstanceSupplier(type, qualifiers); + return createFactory(handleSupplier, fallbackSupplier, type, qualifiers); } - private Factory createFactory(Supplier > handleSupplier, Class type, Annotation... qualifiers) { + private Factory createFactory(Supplier > handleSupplier, Supplier > fallbackSupplier, + Class type, Annotation... qualifiers) { if (handleSupplier == null) { LOGGER.debugf( "No matching bean found for type %s and qualifiers %s. The bean might have been marked as unused and removed during build.", type, Arrays.toString(qualifiers)); - return new DefaultInstanceFactory<>(type); + if (fallbackSupplier != null) { + return fallbackSupplier.get(); + } else { + // by default, if there is no bean, return factory that tries to instantiate non-cdi object + return new DefaultInstanceFactory<>(type); + } } return new Factory () { @Override @@ -64,7 +76,16 @@ public ManagedContext requestContext() { return container.requestContext(); } - private static final class DefaultInstanceFactory implements BeanContainer.Factory { + /** + * A default fallback {@link Factory} implementation used by + * {@link BeanContainer#beanInstanceFactory(Class, Annotation...)}. + * + * This factory attempts to create instances of given class by calling their no-arg constructor. Any exceptions + * related to lack of such constructor of failure to invoke it are simply re-thrown. + * + * @param represents the type that this factory can create + */ + private final class DefaultInstanceFactory implements BeanContainer.Factory { private final Class type; @@ -87,5 +108,4 @@ public T get() { } } } - } diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java index 8baf18521209e..01af040bd7399 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/ArcContainer.java @@ -88,12 +88,6 @@ public interface ArcContainer { /** * Returns a supplier that can be used to create new instances, or null if no matching bean can be found. * - * Note that if there are multiple sub classes of the given type this will return the exact match. This means - * that this can be used to directly instantiate superclasses of other beans without causing problems. This behavior differs - * to standard CDI rules where an ambiguous dependency would exist. - * - * see https://github.com/quarkusio/quarkus/issues/3369 - * * @param type * @param qualifiers * @param @@ -101,21 +95,6 @@ public interface ArcContainer { */ Supplier > beanInstanceSupplier(Class type, Annotation... qualifiers); - /** - * This method is deprecated and will be removed in future versions. - * Use {@link #beanInstanceSupplier(Class, Annotation...)} instead. - * - * As opposed to {@link #beanInstanceSupplier(Class, Annotation...)}, this method does NOT follow CDI - * resolution rules and in case of ambiguous resolution performs a choice based on the class type parameter. - * - * @param type - * @param qualifiers - * @return - * @param - */ - @Deprecated - Supplier > instanceSupplier(Class type, Annotation... qualifiers); - /** * * @param bean diff --git a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java index 12f6a9ad0a65b..a01607c33fc96 100644 --- a/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java +++ b/independent-projects/arc/runtime/src/main/java/io/quarkus/arc/impl/ArcContainerImpl.java @@ -291,16 +291,6 @@ public InstanceHandle instance(Type type, Annotation... qualifiers) { @Override public Supplier > beanInstanceSupplier(Class type, Annotation... qualifiers) { - return createInstanceSupplier(false, type, qualifiers); - } - - @Override - public Supplier > instanceSupplier(Class type, Annotation... qualifiers) { - return createInstanceSupplier(true, type, qualifiers); - } - - private Supplier > createInstanceSupplier(boolean resolveAmbiguities, Class type, - Annotation... qualifiers) { if (qualifiers == null || qualifiers.length == 0) { qualifiers = new Annotation[] { Default.Literal.INSTANCE }; } @@ -311,20 +301,8 @@ private Supplier > createInstanceSupplier(boolean resolveAm } Set > filteredBean = resolvedBeans; if (resolvedBeans.size() > 1) { - if (resolveAmbiguities) { - // this is non-standard CDI behavior that we momentarily keep to retain compatibility - // if there are multiple beans we look for an exact match - // this method is only called with the exact type required - // so ignoring subclasses is the correct behaviour - filteredBean = new HashSet<>(); - for (InjectableBean> i : resolvedBeans) { - if (i.getBeanClass().equals(type)) { - filteredBean.add(i); - } - } - } else { - throw new AmbiguousResolutionException("Beans: " + resolvedBeans); - } + throw new AmbiguousResolutionException("Beans: " + resolvedBeans); + } @SuppressWarnings("unchecked") InjectableBean bean = filteredBean.size() != 1 ? null