Search in sources :

Example 1 with ConstructorInjectionPoint

use of io.micronaut.inject.ConstructorInjectionPoint in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method doCreateBean.

/**
 * Execution the creation of a bean. The returned value can be null if a
 * factory method returned null.
 *
 * @param resolutionContext The {@link BeanResolutionContext}
 * @param beanDefinition    The {@link BeanDefinition}
 * @param qualifier         The {@link Qualifier}
 * @param qualifierBeanType The bean type used in the qualifier
 * @param isSingleton       Whether the bean is a singleton
 * @param argumentValues    Any argument values passed to create the bean
 * @param <T>               The bean generic type
 * @return The created bean
 */
@Nullable
protected <T> T doCreateBean(@NonNull BeanResolutionContext resolutionContext, @NonNull BeanDefinition<T> beanDefinition, @Nullable Qualifier<T> qualifier, @Nullable Argument<T> qualifierBeanType, boolean isSingleton, @Nullable Map<String, Object> argumentValues) {
    if (argumentValues == null) {
        argumentValues = Collections.emptyMap();
    }
    Qualifier<T> declaredQualifier = beanDefinition.getDeclaredQualifier();
    Class<T> beanType = beanDefinition.getBeanType();
    if (isSingleton) {
        BeanRegistration<T> beanRegistration = singletonObjects.get(new BeanKey<>(beanDefinition, declaredQualifier));
        final Class<T> beanClass = qualifierBeanType.getType();
        if (beanRegistration != null) {
            if (qualifier == null || qualifier.reduce(beanClass, Stream.of(beanRegistration.beanDefinition)).findFirst().isPresent()) {
                return beanRegistration.bean;
            }
        } else if (qualifier != null) {
            beanRegistration = singletonObjects.get(new BeanKey<>(beanDefinition, null));
            if (beanRegistration != null && qualifier.reduce(beanClass, Stream.of(beanRegistration.beanDefinition)).findFirst().isPresent()) {
                return beanRegistration.bean;
            }
        }
    }
    T bean;
    if (beanDefinition instanceof BeanFactory) {
        BeanFactory<T> beanFactory = (BeanFactory<T>) beanDefinition;
        try {
            if (beanFactory instanceof ParametrizedBeanFactory) {
                ParametrizedBeanFactory<T> parametrizedBeanFactory = (ParametrizedBeanFactory<T>) beanFactory;
                Argument<?>[] requiredArguments = parametrizedBeanFactory.getRequiredArguments();
                Map<String, Object> convertedValues = new LinkedHashMap<>(argumentValues);
                for (Argument<?> requiredArgument : requiredArguments) {
                    String argumentName = requiredArgument.getName();
                    Object val = argumentValues.get(argumentName);
                    if (val == null && !requiredArgument.isDeclaredNullable()) {
                        throw new BeanInstantiationException(resolutionContext, "Missing bean argument [" + requiredArgument + "] for type: " + beanType.getName() + ". Required arguments: " + ArrayUtils.toString(requiredArguments));
                    }
                    Object convertedValue = null;
                    if (val != null) {
                        if (requiredArgument.getType().isInstance(val)) {
                            convertedValue = val;
                        } else {
                            convertedValue = ConversionService.SHARED.convert(val, requiredArgument).orElseThrow(() -> new BeanInstantiationException(resolutionContext, "Invalid bean argument [" + requiredArgument + "]. Cannot convert object [" + val + "] to required type: " + requiredArgument.getType()));
                        }
                    }
                    convertedValues.put(argumentName, convertedValue);
                }
                bean = parametrizedBeanFactory.build(resolutionContext, this, beanDefinition, convertedValues);
            } else {
                boolean propagateQualifier = beanDefinition.isProxy() && declaredQualifier instanceof Named;
                if (propagateQualifier) {
                    resolutionContext.setAttribute(BeanDefinition.NAMED_ATTRIBUTE, ((Named) declaredQualifier).getName());
                }
                resolutionContext.setCurrentQualifier(declaredQualifier != null ? declaredQualifier : qualifier);
                try {
                    bean = beanFactory.build(resolutionContext, this, beanDefinition);
                } finally {
                    resolutionContext.setCurrentQualifier(null);
                    if (propagateQualifier) {
                        resolutionContext.removeAttribute(BeanDefinition.NAMED_ATTRIBUTE);
                    }
                }
                if (bean == null) {
                    throw new BeanInstantiationException(resolutionContext, "Bean Factory [" + beanFactory + "] returned null");
                } else {
                    if (bean instanceof Qualified) {
                        ((Qualified) bean).$withBeanQualifier(declaredQualifier);
                    }
                }
            }
        } catch (Throwable e) {
            if (e instanceof DependencyInjectionException) {
                throw e;
            }
            if (e instanceof DisabledBeanException) {
                throw e;
            }
            if (e instanceof BeanInstantiationException) {
                throw e;
            } else {
                if (!resolutionContext.getPath().isEmpty()) {
                    throw new BeanInstantiationException(resolutionContext, e);
                } else {
                    throw new BeanInstantiationException(beanDefinition, e);
                }
            }
        }
    } else {
        ConstructorInjectionPoint<T> constructor = beanDefinition.getConstructor();
        Argument<?>[] requiredConstructorArguments = constructor.getArguments();
        if (requiredConstructorArguments.length == 0) {
            bean = constructor.invoke();
        } else {
            Object[] constructorArgs = new Object[requiredConstructorArguments.length];
            for (int i = 0; i < requiredConstructorArguments.length; i++) {
                Class<?> argument = requiredConstructorArguments[i].getType();
                constructorArgs[i] = getBean(resolutionContext, argument);
            }
            bean = constructor.invoke(constructorArgs);
        }
        inject(resolutionContext, null, bean);
    }
    if (bean != null) {
        Qualifier<T> finalQualifier = qualifier != null ? qualifier : declaredQualifier;
        if (!(bean instanceof BeanCreatedEventListener) && CollectionUtils.isNotEmpty(beanCreationEventListeners)) {
            for (Map.Entry<Class, List<BeanCreatedEventListener>> entry : beanCreationEventListeners) {
                if (entry.getKey().isAssignableFrom(beanType)) {
                    BeanKey<T> beanKey = new BeanKey<>(beanDefinition, finalQualifier);
                    for (BeanCreatedEventListener<?> listener : entry.getValue()) {
                        bean = (T) listener.onCreated(new BeanCreatedEvent(this, beanDefinition, beanKey, bean));
                        if (bean == null) {
                            throw new BeanInstantiationException(resolutionContext, "Listener [" + listener + "] returned null from onCreated event");
                        }
                    }
                }
            }
        }
        if (beanDefinition instanceof ValidatedBeanDefinition) {
            bean = ((ValidatedBeanDefinition<T>) beanDefinition).validate(resolutionContext, bean);
        }
        if (LOG_LIFECYCLE.isDebugEnabled()) {
            LOG_LIFECYCLE.debug("Created bean [{}] from definition [{}] with qualifier [{}]", bean, beanDefinition, finalQualifier);
        }
    }
    return bean;
}
Also used : Argument(io.micronaut.core.type.Argument) BeanCreatedEvent(io.micronaut.context.event.BeanCreatedEvent) LinkedHashMap(java.util.LinkedHashMap) ConcurrentLinkedHashMap(io.micronaut.core.util.clhm.ConcurrentLinkedHashMap) DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) BeanFactory(io.micronaut.inject.BeanFactory) ParametrizedBeanFactory(io.micronaut.inject.ParametrizedBeanFactory) ParametrizedBeanFactory(io.micronaut.inject.ParametrizedBeanFactory) ArrayList(java.util.ArrayList) List(java.util.List) Named(io.micronaut.core.naming.Named) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) Qualified(io.micronaut.inject.qualifiers.Qualified) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisabledBeanException(io.micronaut.context.exceptions.DisabledBeanException) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) ConstructorInjectionPoint(io.micronaut.inject.ConstructorInjectionPoint) InjectionPoint(io.micronaut.inject.InjectionPoint) MethodInjectionPoint(io.micronaut.inject.MethodInjectionPoint) BeanCreatedEventListener(io.micronaut.context.event.BeanCreatedEventListener) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentLinkedHashMap(io.micronaut.core.util.clhm.ConcurrentLinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Nullable(io.micronaut.core.annotation.Nullable)

Aggregations

BeanCreatedEvent (io.micronaut.context.event.BeanCreatedEvent)1 BeanCreatedEventListener (io.micronaut.context.event.BeanCreatedEventListener)1 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)1 DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 Nullable (io.micronaut.core.annotation.Nullable)1 Named (io.micronaut.core.naming.Named)1 Argument (io.micronaut.core.type.Argument)1 ConcurrentLinkedHashMap (io.micronaut.core.util.clhm.ConcurrentLinkedHashMap)1 BeanFactory (io.micronaut.inject.BeanFactory)1 ConstructorInjectionPoint (io.micronaut.inject.ConstructorInjectionPoint)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 InjectionPoint (io.micronaut.inject.InjectionPoint)1 MethodInjectionPoint (io.micronaut.inject.MethodInjectionPoint)1 ParametrizedBeanFactory (io.micronaut.inject.ParametrizedBeanFactory)1 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)1 Qualified (io.micronaut.inject.qualifiers.Qualified)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1