Search in sources :

Example 1 with DependencyInjectionException

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

the class AbstractInitializableBeanDefinition method setFieldWithReflection.

/**
 * Sets the value of a field of a object that requires reflection.
 *
 * @param resolutionContext The resolution context
 * @param context           The object context
 * @param index             The index of the field
 * @param object            The object whose field should be modifie
 * @param value             The instance being set
 */
@SuppressWarnings("unused")
@Internal
protected final void setFieldWithReflection(BeanResolutionContext resolutionContext, BeanContext context, int index, Object object, Object value) {
    FieldReference fieldRef = fieldInjection[index];
    try {
        if (ClassUtils.REFLECTION_LOGGER.isDebugEnabled()) {
            ClassUtils.REFLECTION_LOGGER.debug("Bean of type [" + getBeanType() + "] uses reflection to inject field: '" + fieldRef.argument.getName() + "'");
        }
        Field field = ReflectionUtils.getRequiredField(fieldRef.declaringType, fieldRef.argument.getName());
        field.setAccessible(true);
        field.set(object, value);
    } catch (Throwable e) {
        if (e instanceof BeanContextException) {
            throw (BeanContextException) e;
        } else {
            throw new DependencyInjectionException(resolutionContext, "Error setting field value: " + e.getMessage(), e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) BeanContextException(io.micronaut.context.exceptions.BeanContextException) Internal(io.micronaut.core.annotation.Internal)

Example 2 with DependencyInjectionException

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

the class AbstractInitializableBeanDefinition method getPropertyPlaceholderValueForConstructorArgument.

/**
 * 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
 * @return The resolved bean
 */
@Internal
@UsedByGeneratedCode
protected final Object getPropertyPlaceholderValueForConstructorArgument(BeanResolutionContext resolutionContext, BeanContext context, int argIndex, String propertyValue) {
    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, null, true);
            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 3 with DependencyInjectionException

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

the class AbstractInitializableBeanDefinition method resolveBeanRegistrations.

private <B> Collection<BeanRegistration<B>> resolveBeanRegistrations(BeanResolutionContext resolutionContext, BeanContext beanContext, Argument argument, Argument genericArgument, Qualifier qualifier) {
    try {
        if (genericArgument == null) {
            throw new DependencyInjectionException(resolutionContext, "Cannot resolve bean registrations. Argument [" + argument + "] missing generic type information.");
        }
        qualifier = qualifier == null ? resolveQualifier(resolutionContext, argument) : qualifier;
        Collection beanRegistrations = ((DefaultBeanContext) beanContext).getBeanRegistrations(resolutionContext, genericArgument, qualifier);
        return coerceCollectionToCorrectType(argument.getType(), beanRegistrations, resolutionContext, argument);
    } catch (NoSuchBeanException e) {
        if (argument.isNullable()) {
            return null;
        }
        throw new DependencyInjectionException(resolutionContext, e);
    }
}
Also used : DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) NoSuchBeanException(io.micronaut.context.exceptions.NoSuchBeanException) Collection(java.util.Collection)

Example 4 with DependencyInjectionException

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

the class AbstractInitializableBeanDefinition method invokeMethodWithReflection.

/**
 * Invoke a bean method that requires reflection.
 *
 * @param resolutionContext The resolution context
 * @param context           The bean context
 * @param methodIndex       The method index
 * @param bean              The bean
 * @param methodArgs        The method args
 */
@Internal
@SuppressWarnings("WeakerAccess")
protected final void invokeMethodWithReflection(BeanResolutionContext resolutionContext, BeanContext context, int methodIndex, Object bean, Object[] methodArgs) {
    MethodReference methodRef = methodInjection[methodIndex];
    Argument[] methodArgumentTypes = methodRef.arguments == null ? Argument.ZERO_ARGUMENTS : methodRef.arguments;
    if (ClassUtils.REFLECTION_LOGGER.isDebugEnabled()) {
        ClassUtils.REFLECTION_LOGGER.debug("Bean of type [" + getBeanType() + "] uses reflection to inject method: '" + methodRef.methodName + "'");
    }
    try {
        Method method = ReflectionUtils.getMethod(methodRef.declaringType, methodRef.methodName, Argument.toClassArray(methodArgumentTypes)).orElseThrow(() -> ReflectionUtils.newNoSuchMethodError(methodRef.declaringType, methodRef.methodName, Argument.toClassArray(methodArgumentTypes)));
        method.setAccessible(true);
        ReflectionUtils.invokeMethod(bean, method, methodArgs);
    } catch (Throwable e) {
        if (e instanceof BeanContextException) {
            throw (BeanContextException) e;
        } else {
            throw new DependencyInjectionException(resolutionContext, "Error invoking method: " + methodRef.methodName, e);
        }
    }
}
Also used : DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) DefaultArgument(io.micronaut.core.type.DefaultArgument) Argument(io.micronaut.core.type.Argument) BeanContextException(io.micronaut.context.exceptions.BeanContextException) Method(java.lang.reflect.Method) ExecutableMethod(io.micronaut.inject.ExecutableMethod) Internal(io.micronaut.core.annotation.Internal)

Example 5 with DependencyInjectionException

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

the class AbstractInitializableBeanDefinition method resolveBeansOfType.

private Collection<Object> resolveBeansOfType(BeanResolutionContext resolutionContext, BeanContext context, Argument argument, Argument resultGenericType, Qualifier qualifier) {
    DefaultBeanContext beanContext = (DefaultBeanContext) context;
    if (resultGenericType == null) {
        throw new DependencyInjectionException(resolutionContext, "Type " + argument.getType() + " has no generic argument");
    }
    qualifier = qualifier == null ? resolveQualifier(resolutionContext, argument, true) : qualifier;
    Collection beansOfType = beanContext.getBeansOfType(resolutionContext, resolveEnvironmentArgument(context, resultGenericType), qualifier);
    return coerceCollectionToCorrectType(argument.getType(), beansOfType, resolutionContext, argument);
}
Also used : DependencyInjectionException(io.micronaut.context.exceptions.DependencyInjectionException) Collection(java.util.Collection)

Aggregations

DependencyInjectionException (io.micronaut.context.exceptions.DependencyInjectionException)11 NoSuchBeanException (io.micronaut.context.exceptions.NoSuchBeanException)5 Internal (io.micronaut.core.annotation.Internal)5 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)5 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)4 BeanContextException (io.micronaut.context.exceptions.BeanContextException)3 UsedByGeneratedCode (io.micronaut.core.annotation.UsedByGeneratedCode)3 Collection (java.util.Collection)3 Argument (io.micronaut.core.type.Argument)2 ApplicationContext (io.micronaut.context.ApplicationContext)1 Value (io.micronaut.context.annotation.Value)1 BeanCreatedEvent (io.micronaut.context.event.BeanCreatedEvent)1 BeanCreatedEventListener (io.micronaut.context.event.BeanCreatedEventListener)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 NonUniqueBeanException (io.micronaut.context.exceptions.NonUniqueBeanException)1 Nullable (io.micronaut.core.annotation.Nullable)1 Bindable (io.micronaut.core.bind.annotation.Bindable)1 ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)1 Named (io.micronaut.core.naming.Named)1 DefaultArgument (io.micronaut.core.type.DefaultArgument)1