Search in sources :

Example 1 with AnyQualifier

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");
}
Also used : Named(io.micronaut.core.naming.Named) Argument(io.micronaut.core.type.Argument) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) ArgumentCoercible(io.micronaut.core.type.ArgumentCoercible) DisabledBeanException(io.micronaut.context.exceptions.DisabledBeanException) AnyQualifier(io.micronaut.inject.qualifiers.AnyQualifier) Qualifier(io.micronaut.context.Qualifier) AnyQualifier(io.micronaut.inject.qualifiers.AnyQualifier) BeanResolutionContext(io.micronaut.context.BeanResolutionContext)

Aggregations

BeanResolutionContext (io.micronaut.context.BeanResolutionContext)1 Qualifier (io.micronaut.context.Qualifier)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)1 Named (io.micronaut.core.naming.Named)1 Argument (io.micronaut.core.type.Argument)1 ArgumentCoercible (io.micronaut.core.type.ArgumentCoercible)1 AnyQualifier (io.micronaut.inject.qualifiers.AnyQualifier)1