Search in sources :

Example 1 with NoUniqueBeanDefinitionException

use of cn.taketoday.beans.factory.NoUniqueBeanDefinitionException in project today-framework by TAKETODAY.

the class ConstructorResolver method resolveAutowiredArgument.

/**
 * Template method for resolving the specified argument which is supposed to be autowired.
 */
@Nullable
private Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter, boolean fallback) {
    Class<?> paramType = param.getParameterType();
    if (InjectionPoint.class.isAssignableFrom(paramType)) {
        InjectionPoint injectionPoint = currentInjectionPoint.get();
        if (injectionPoint == null) {
            throw new IllegalStateException("No current InjectionPoint available for " + param);
        }
        return injectionPoint;
    }
    try {
        DependencyResolvingContext context = new DependencyResolvingContext(param.getExecutable(), beanFactory, beanName);
        context.setTypeConverter(typeConverter);
        context.setDependentBeans(autowiredBeanNames);
        return injector.resolveValue(new DependencyDescriptor(param, true), context);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw ex;
    } catch (NoSuchBeanDefinitionException ex) {
        if (fallback) {
            // for e.g. a vararg or a non-null List/Set/Map parameter.
            if (paramType.isArray()) {
                return Array.newInstance(paramType.getComponentType(), 0);
            } else if (CollectionUtils.isApproximableCollectionType(paramType)) {
                return CollectionUtils.createCollection(paramType, 0);
            } else if (CollectionUtils.isApproximableMapType(paramType)) {
                return CollectionUtils.createMap(paramType, 0);
            }
        }
        throw ex;
    }
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Nullable(cn.taketoday.lang.Nullable)

Example 2 with NoUniqueBeanDefinitionException

use of cn.taketoday.beans.factory.NoUniqueBeanDefinitionException in project today-framework by TAKETODAY.

the class NoSuchBeanDefinitionFailureAnalyzerTests method failureAnalysisForMultipleBeans.

@Test
void failureAnalysisForMultipleBeans() {
    FailureAnalysis analysis = analyzeFailure(new NoUniqueBeanDefinitionException(String.class, 2, "Test"));
    assertThat(analysis).isNull();
}
Also used : FailureAnalysis(cn.taketoday.framework.diagnostics.FailureAnalysis) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Test(org.junit.jupiter.api.Test)

Example 3 with NoUniqueBeanDefinitionException

use of cn.taketoday.beans.factory.NoUniqueBeanDefinitionException in project today-infrastructure by TAKETODAY.

the class NoSuchBeanDefinitionFailureAnalyzerTests method failureAnalysisForMultipleBeans.

@Test
void failureAnalysisForMultipleBeans() {
    FailureAnalysis analysis = analyzeFailure(new NoUniqueBeanDefinitionException(String.class, 2, "Test"));
    assertThat(analysis).isNull();
}
Also used : FailureAnalysis(cn.taketoday.framework.diagnostics.FailureAnalysis) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Test(org.junit.jupiter.api.Test)

Example 4 with NoUniqueBeanDefinitionException

use of cn.taketoday.beans.factory.NoUniqueBeanDefinitionException in project today-infrastructure by TAKETODAY.

the class ConstructorResolver method resolveAutowiredArgument.

/**
 * Template method for resolving the specified argument which is supposed to be autowired.
 */
@Nullable
private Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter, boolean fallback) {
    Class<?> paramType = param.getParameterType();
    if (InjectionPoint.class.isAssignableFrom(paramType)) {
        InjectionPoint injectionPoint = currentInjectionPoint.get();
        if (injectionPoint == null) {
            throw new IllegalStateException("No current InjectionPoint available for " + param);
        }
        return injectionPoint;
    }
    try {
        DependencyResolvingContext context = new DependencyResolvingContext(param.getExecutable(), beanFactory, beanName);
        context.setTypeConverter(typeConverter);
        context.setDependentBeans(autowiredBeanNames);
        return injector.resolveValue(new DependencyDescriptor(param, true), context);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw ex;
    } catch (NoSuchBeanDefinitionException ex) {
        if (fallback) {
            // for e.g. a vararg or a non-null List/Set/Map parameter.
            if (paramType.isArray()) {
                return Array.newInstance(paramType.getComponentType(), 0);
            } else if (CollectionUtils.isApproximableCollectionType(paramType)) {
                return CollectionUtils.createCollection(paramType, 0);
            } else if (CollectionUtils.isApproximableMapType(paramType)) {
                return CollectionUtils.createMap(paramType, 0);
            }
        }
        throw ex;
    }
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Nullable(cn.taketoday.lang.Nullable)

Aggregations

NoUniqueBeanDefinitionException (cn.taketoday.beans.factory.NoUniqueBeanDefinitionException)4 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)2 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)2 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)2 FailureAnalysis (cn.taketoday.framework.diagnostics.FailureAnalysis)2 Nullable (cn.taketoday.lang.Nullable)2 Test (org.junit.jupiter.api.Test)2