From bc304235a67081c7c0167410e0a780410dfe28a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Rodi=C3=A8re?= Date: Tue, 19 Dec 2017 15:52:52 +0100 Subject: [PATCH] HHH-12133 Use the BeanManager-provided ambiguous dependency resolution when possible This should take care of @Alternative in particular. --- .../resource/beans/internal/Helper.java | 22 ------------------- .../JpaCdiLifecycleManagementStrategy.java | 5 ++++- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java index 9f0768e642..630491ae52 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/Helper.java @@ -6,10 +6,6 @@ */ package org.hibernate.resource.beans.internal; -import java.util.Set; -import javax.enterprise.inject.spi.Bean; -import javax.enterprise.inject.spi.BeanManager; - /** * @author Steve Ebersole */ @@ -22,24 +18,6 @@ public class Helper { private Helper() { } - @SuppressWarnings("unchecked") - public Bean getNamedBean(String beanName, Class beanContract, BeanManager beanManager) { - Set> beans = beanManager.getBeans( beanContract, new NamedBeanQualifier( beanName ) ); - - if ( beans.isEmpty() ) { - throw new IllegalArgumentException( - "BeanManager returned no matching beans: name = " + beanName + "; contract = " + beanContract.getName() - ); - } - if ( beans.size() > 1 ) { - throw new IllegalArgumentException( - "BeanManager returned multiple matching beans: name = " + beanName + "; contract = " + beanContract.getName() - ); - } - - return (Bean) beans.iterator().next(); - } - public CdiLifecycleManagementStrategy getLifecycleManagementStrategy(boolean shouldRegistryManageLifecycle) { if ( shouldRegistryManageLifecycle ) { return JpaCdiLifecycleManagementStrategy.INSTANCE; diff --git a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java index 48268abe90..565a7c0b7f 100644 --- a/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java +++ b/hibernate-core/src/main/java/org/hibernate/resource/beans/internal/JpaCdiLifecycleManagementStrategy.java @@ -6,6 +6,7 @@ */ package org.hibernate.resource.beans.internal; +import java.util.Set; import javax.enterprise.context.spi.CreationalContext; import javax.enterprise.inject.spi.AnnotatedType; import javax.enterprise.inject.spi.Bean; @@ -48,9 +49,11 @@ class JpaCdiLifecycleManagementStrategy implements CdiLifecycleManagementStrateg return new JpaManagedBeanImpl<>( beanClass, injectionTarget, creationalContext, beanInstance ); } + @SuppressWarnings("unchecked") @Override public ManagedBean createBean(BeanManager beanManager, String beanName, Class beanClass) { - Bean bean = Helper.INSTANCE.getNamedBean( beanName, beanClass, beanManager ); + Set> beans = beanManager.getBeans( beanClass, new NamedBeanQualifier( beanName ) ); + Bean bean = (Bean) beanManager.resolve( beans ); CreationalContext creationalContext = beanManager.createCreationalContext( null );