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);
}
}
}
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;
}
}
}
}
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);
}
Aggregations