Search in sources :

Example 11 with DependencyInjectionException

use of io.micronaut.context.exceptions.DependencyInjectionException in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method getBeanInternal.

private <T> T getBeanInternal(@Nullable BeanResolutionContext resolutionContext, @NonNull Argument<T> beanType, Qualifier<T> qualifier, boolean throwNonUnique, boolean throwNoSuchBean) {
    // allow injection the bean context
    final Class<T> beanClass = beanType.getType();
    if (thisInterfaces.contains(beanClass)) {
        return (T) this;
    }
    if (InjectionPoint.class.isAssignableFrom(beanClass)) {
        final BeanResolutionContext.Path path = resolutionContext != null ? resolutionContext.getPath() : null;
        if (CollectionUtils.isNotEmpty(path)) {
            final Iterator<BeanResolutionContext.Segment<?>> i = path.iterator();
            final BeanResolutionContext.Segment<?> injectionPointSegment = i.next();
            if (i.hasNext()) {
                BeanResolutionContext.Segment segment = i.next();
                final BeanDefinition declaringBean = segment.getDeclaringType();
                if (declaringBean.hasStereotype(INTRODUCTION_TYPE)) {
                    if (!i.hasNext()) {
                        if (!injectionPointSegment.getArgument().isNullable()) {
                            throw new BeanContextException("Failed to obtain injection point. No valid injection path present in path: " + path);
                        } else {
                            return null;
                        }
                    } else {
                        segment = i.next();
                    }
                }
                T ip = (T) segment.getInjectionPoint();
                if (beanClass.isInstance(ip)) {
                    return ip;
                } else {
                    if (!injectionPointSegment.getArgument().isNullable()) {
                        throw new DependencyInjectionException(resolutionContext, "Failed to obtain injection point. No valid injection path present in path: " + path);
                    } else {
                        return null;
                    }
                }
            } else {
                if (!injectionPointSegment.getArgument().isNullable()) {
                    throw new DependencyInjectionException(resolutionContext, "Failed to obtain injection point. No valid injection path present in path: " + path);
                } else {
                    return null;
                }
            }
        } else {
            throw new DependencyInjectionException(resolutionContext, "Failed to obtain injection point. No valid injection path present in path: " + path);
        }
    }
    BeanKey<T> beanKey = new BeanKey<>(beanType, qualifier);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Looking up existing bean for key: {}", beanKey);
    }
    T inFlightBean = resolutionContext != null ? resolutionContext.getInFlightBean(beanKey) : null;
    if (inFlightBean != null) {
        return inFlightBean;
    }
    BeanRegistration<T> beanRegistration = singletonObjects.get(beanKey);
    if (beanRegistration != null) {
        T bean = beanRegistration.bean;
        if (bean == null && throwNoSuchBean) {
            throw new NoSuchBeanException(beanType, qualifier);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Resolved existing bean [{}] for type [{}] and qualifier [{}]", beanRegistration.bean, beanType, qualifier);
            }
            return bean;
        }
    } else if (LOG.isTraceEnabled()) {
        LOG.trace("No existing bean found for bean key: {}", beanKey);
    }
    synchronized (singletonObjects) {
        Optional<BeanDefinition<T>> concreteCandidate = findConcreteCandidate(resolutionContext, beanType, qualifier, throwNonUnique);
        T bean = null;
        if (concreteCandidate.isPresent()) {
            BeanDefinition<T> definition = concreteCandidate.get();
            if (definition.isContainerType() && beanClass != definition.getBeanType()) {
                throw new NonUniqueBeanException(beanClass, Collections.singletonList(definition).iterator());
            }
            if (definition.isSingleton()) {
                final BeanRegistration<T> registration = findExistingCompatibleSingleton(definition.asArgument(), beanType, qualifier, definition);
                if (registration != null) {
                    bean = registration.getBean();
                }
            }
            if (bean != null) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Resolved existing bean [{}] for type [{}] and qualifier [{}]", bean, beanType, qualifier);
                }
                return bean;
            } else {
                bean = getBeanForDefinition(resolutionContext, beanType, qualifier, throwNoSuchBean, definition);
                if (bean == null && throwNoSuchBean) {
                    throw new NoSuchBeanException(beanType, qualifier);
                } else {
                    return bean;
                }
            }
        } else {
            final BeanRegistration<T> registration = findExistingCompatibleSingleton(beanType, null, qualifier, null);
            if (registration != null) {
                bean = registration.getBean();
            }
            if (bean == null && throwNoSuchBean) {
                throw new NoSuchBeanException(beanType, qualifier);
            } else {
                return bean;
            }
        }
    }
}
Also used : NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) NonUniqueBeanException(io.micronaut.context.exceptions.NonUniqueBeanException) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition) DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) BeanContextException(io.micronaut.context.exceptions.BeanContextException)

Aggregations

DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)11 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)5 Internal (io.micronaut.core.annotation.Internal)5 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)5 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)4 BeanContextException (io.micronaut.context.exceptions.BeanContextException)3 UsedByGeneratedCode (io.micronaut.core.annotation.UsedByGeneratedCode)3 Collection (java.util.Collection)3 Argument (io.micronaut.core.type.Argument)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 Value (io.micronaut.context.annotation.Value)1 BeanCreatedEvent (io.micronaut.context.event.BeanCreatedEvent)1 BeanCreatedEventListener (io.micronaut.context.event.BeanCreatedEventListener)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 NonUniqueBeanException (io.micronaut.context.exceptions.NonUniqueBeanException)1 Nullable (io.micronaut.core.annotation.Nullable)1 Bindable (io.micronaut.core.bind.annotation.Bindable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 Named (io.micronaut.core.naming.Named)1 DefaultArgument (io.micronaut.core.type.DefaultArgument)1