use of io.micronaut.inject.qualifiers.AnyQualifier in project micronaut-core by micronaut-projects.
the class AbstractProviderDefinition method build.
@Override
public T build(BeanResolutionContext resolutionContext, BeanContext context, BeanDefinition<T> definition) throws BeanInstantiationException {
final BeanResolutionContext.Segment<?> segment = resolutionContext.getPath().currentSegment().orElse(null);
if (segment != null) {
final InjectionPoint<?> injectionPoint = segment.getInjectionPoint();
if (injectionPoint instanceof ArgumentCoercible) {
Argument<?> injectionPointArgument = ((ArgumentCoercible<?>) injectionPoint).asArgument();
Argument<?> resolveArgument = injectionPointArgument;
if (resolveArgument.isOptional()) {
resolveArgument = resolveArgument.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
}
@SuppressWarnings("unchecked") Argument<Object> argument = (Argument<Object>) resolveArgument.getFirstTypeVariable().orElse(null);
if (argument != null) {
Qualifier<Object> qualifier = (Qualifier<Object>) resolutionContext.getCurrentQualifier();
if (qualifier == null && segment.getDeclaringType().isIterable()) {
final Object n = resolutionContext.getAttribute(Named.class.getName());
if (n != null) {
qualifier = Qualifiers.byName(n.toString());
}
}
boolean hasBean = context.containsBean(argument, qualifier);
if (hasBean) {
return buildProvider(resolutionContext, context, argument, qualifier, definition.isSingleton());
} else {
if (injectionPointArgument.isOptional()) {
return (T) Optional.empty();
} else if (injectionPointArgument.isNullable()) {
throw new DisabledBeanException("Nullable bean doesn't exist");
} else {
if (qualifier instanceof AnyQualifier || isAllowEmptyProviders(context)) {
return buildProvider(resolutionContext, context, argument, qualifier, definition.isSingleton());
} else {
throw new NoSuchBeanException(argument, qualifier);
}
}
}
}
}
}
throw new UnsupportedOperationException("Cannot inject provider for Object type");
}
Aggregations