Search in sources :

Example 6 with NoSuchBeanException

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

the class AbstractInitializableBeanDefinition method getPropertyValueForConstructorArgument.

/**
 * Obtains a property value for a bean definition for a constructor at the given index
 * <p>
 * Warning: this method is used by internal generated code and should not be called by user code.
 *
 * @param resolutionContext The resolution context
 * @param context           The context
 * @param argIndex          The argument index
 * @param propertyValue     The property value
 * @param cliProperty       The cli property
 * @return The resolved bean
 */
@Internal
@UsedByGeneratedCode
protected final Object getPropertyValueForConstructorArgument(BeanResolutionContext resolutionContext, BeanContext context, int argIndex, String propertyValue, String cliProperty) {
    MethodReference constructorRef = (MethodReference) constructor;
    Argument<?> argument = constructorRef.arguments[argIndex];
    try (BeanResolutionContext.Path ignored = resolutionContext.getPath().pushConstructorResolve(this, argument)) {
        try {
            Object result = resolvePropertyValue(resolutionContext, context, argument, propertyValue, cliProperty, false);
            if (this instanceof ValidatedBeanDefinition) {
                ((ValidatedBeanDefinition) this).validateBeanArgument(resolutionContext, getConstructor(), argument, argIndex, result);
            }
            return result;
        } catch (NoSuchBeanException | BeanInstantiationException e) {
            throw new DependencyInjectionException(resolutionContext, argument, e);
        }
    }
}
Also used : DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) Internal(io.micronaut.core.annotation.Internal) UsedByGeneratedCode(io.micronaut.core.annotation.UsedByGeneratedCode)

Example 7 with NoSuchBeanException

use of io.micronaut.context.exceptions.NoSuchBeanException 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)

Example 8 with NoSuchBeanException

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

the class DefaultBeanContext method findConcreteCandidateNoCache.

private <T> Optional<BeanDefinition<T>> findConcreteCandidateNoCache(BeanResolutionContext resolutionContext, Argument<T> beanType, Qualifier<T> qualifier, boolean throwNonUnique, boolean filterProxied) {
    Predicate<BeanDefinition<T>> predicate = new Predicate<BeanDefinition<T>>() {

        @Override
        public boolean test(BeanDefinition<T> candidate) {
            if (candidate.isAbstract()) {
                return false;
            }
            if (qualifier != null) {
                if (candidate instanceof NoInjectionBeanDefinition) {
                    NoInjectionBeanDefinition noInjectionBeanDefinition = (NoInjectionBeanDefinition) candidate;
                    return qualifier.contains(noInjectionBeanDefinition.qualifier);
                }
            }
            return true;
        }
    };
    Collection<BeanDefinition<T>> candidates = new ArrayList<>(findBeanCandidates(resolutionContext, beanType, filterProxied, predicate));
    if (candidates.isEmpty()) {
        return Optional.empty();
    }
    filterProxiedTypes(candidates, filterProxied, false, predicate);
    int size = candidates.size();
    BeanDefinition<T> definition = null;
    if (size > 0) {
        if (qualifier != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Qualifying bean [{}] for qualifier: {} ", beanType.getName(), qualifier);
            }
            Stream<BeanDefinition<T>> candidateStream = candidates.stream().filter(c -> {
                if (!c.isAbstract()) {
                    if (c instanceof NoInjectionBeanDefinition) {
                        NoInjectionBeanDefinition noInjectionBeanDefinition = (NoInjectionBeanDefinition) c;
                        return qualifier.contains(noInjectionBeanDefinition.qualifier);
                    }
                    return true;
                }
                return false;
            });
            Stream<BeanDefinition<T>> qualified = qualifier.reduce(beanType.getType(), candidateStream);
            List<BeanDefinition<T>> beanDefinitionList = qualified.collect(Collectors.toList());
            if (beanDefinitionList.isEmpty()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("No qualifying beans of type [{}] found for qualifier: {} ", beanType.getName(), qualifier);
                }
                return Optional.empty();
            }
            definition = lastChanceResolve(beanType, qualifier, throwNonUnique, beanDefinitionList);
        } else {
            if (candidates.size() == 1) {
                definition = candidates.iterator().next();
            } else {
                if (candidates.isEmpty()) {
                    throw new NoSuchBeanException(beanType, null);
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Searching for @Primary for type [{}] from candidates: {} ", beanType.getName(), candidates);
                    }
                    definition = lastChanceResolve(beanType, qualifier, throwNonUnique, candidates);
                }
            }
        }
    }
    if (LOG.isDebugEnabled() && definition != null) {
        if (qualifier != null) {
            LOG.debug("Found concrete candidate [{}] for type: {} {} ", definition, qualifier, beanType.getName());
        } else {
            LOG.debug("Found concrete candidate [{}] for type: {} ", definition, beanType.getName());
        }
    }
    return Optional.ofNullable(definition);
}
Also used : NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) ArrayList(java.util.ArrayList) 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) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) ConstructorInjectionPoint(io.micronaut.inject.ConstructorInjectionPoint) InjectionPoint(io.micronaut.inject.InjectionPoint) MethodInjectionPoint(io.micronaut.inject.MethodInjectionPoint) Predicate(java.util.function.Predicate)

Aggregations

NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)8 DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)5 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)5 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)4 Internal (io.micronaut.core.annotation.Internal)3 UsedByGeneratedCode (io.micronaut.core.annotation.UsedByGeneratedCode)3 Argument (io.micronaut.core.type.Argument)2 BeanDefinition (io.micronaut.inject.BeanDefinition)2 ConstructorInjectionPoint (io.micronaut.inject.ConstructorInjectionPoint)2 DisposableBeanDefinition (io.micronaut.inject.DisposableBeanDefinition)2 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)2 InitializingBeanDefinition (io.micronaut.inject.InitializingBeanDefinition)2 InjectionPoint (io.micronaut.inject.InjectionPoint)2 MethodInjectionPoint (io.micronaut.inject.MethodInjectionPoint)2 ProxyBeanDefinition (io.micronaut.inject.ProxyBeanDefinition)2 BeanResolutionContext (io.micronaut.context.BeanResolutionContext)1 Qualifier (io.micronaut.context.Qualifier)1 BeanContextException (io.micronaut.context.exceptions.BeanContextException)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 NonUniqueBeanException (io.micronaut.context.exceptions.NonUniqueBeanException)1