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