Search in sources :

Example 1 with InjectionPoint

use of cn.taketoday.beans.factory.InjectionPoint in project today-infrastructure by TAKETODAY.

the class ConstructorResolver method createArgumentArray.

/**
 * Create an array of arguments to invoke a constructor or factory method,
 * given the resolved constructor argument values.
 */
private ArgumentsHolder createArgumentArray(String beanName, RootBeanDefinition merged, @Nullable ConstructorArgumentValues resolvedValues, Class<?>[] paramTypes, @Nullable String[] paramNames, Executable executable, BeanWrapper wrapper, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : wrapper;
    ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
    HashSet<ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
    LinkedHashSet<String> autowiredBeanNames = new LinkedHashSet<>(4);
    for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
        Class<?> paramType = paramTypes[paramIndex];
        String paramName = paramNames != null ? paramNames[paramIndex] : "";
        // Try to find matching constructor argument value, either indexed or generic.
        ValueHolder valueHolder = null;
        if (resolvedValues != null) {
            valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders);
            // it could match after type conversion (for example, String -> int).
            if (valueHolder == null && (!autowiring || paramTypes.length == resolvedValues.getArgumentCount())) {
                valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders);
            }
        }
        if (valueHolder != null) {
            // We found a potential match - let's give it a try.
            // Do not consider the same value definition multiple times!
            usedValueHolders.add(valueHolder);
            Object originalValue = valueHolder.getValue();
            Object convertedValue;
            if (valueHolder.isConverted()) {
                convertedValue = valueHolder.getConvertedValue();
                args.preparedArguments[paramIndex] = convertedValue;
            } else {
                MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
                try {
                    convertedValue = converter.convertIfNecessary(originalValue, paramType, methodParam);
                } catch (TypeMismatchException ex) {
                    throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(valueHolder.getValue()) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
                }
                Object sourceHolder = valueHolder.getSource();
                if (sourceHolder instanceof ValueHolder) {
                    Object sourceValue = ((ValueHolder) sourceHolder).getValue();
                    args.resolveNecessary = true;
                    args.preparedArguments[paramIndex] = sourceValue;
                }
            }
            args.arguments[paramIndex] = convertedValue;
            args.rawArguments[paramIndex] = originalValue;
        } else {
            MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
            // have to fail creating an argument array for the given constructor.
            if (!autowiring) {
                throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Ambiguous argument values for parameter of type [" + paramType.getName() + "] - did you specify the correct bean references as arguments?");
            }
            try {
                Object autowiredArgument = resolveAutowiredArgument(methodParam, beanName, autowiredBeanNames, converter, fallback);
                args.resolveNecessary = true;
                args.arguments[paramIndex] = autowiredArgument;
                args.rawArguments[paramIndex] = autowiredArgument;
                args.preparedArguments[paramIndex] = autowiredArgumentMarker;
            } catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), ex);
            }
        }
    }
    for (String autowiredBeanName : autowiredBeanNames) {
        beanFactory.registerDependentBean(autowiredBeanName, beanName);
        if (log.isDebugEnabled()) {
            log.debug("Autowiring by type from bean name '{}' via {} to bean named '{}'", beanName, (executable instanceof Constructor ? "constructor" : "factory method"), autowiredBeanName);
        }
    }
    return args;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) Constructor(java.lang.reflect.Constructor) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) ValueHolder(cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeansException(cn.taketoday.beans.BeansException)

Example 2 with InjectionPoint

use of cn.taketoday.beans.factory.InjectionPoint in project today-framework by TAKETODAY.

the class ConstructorResolver method resolvePreparedArguments.

/**
 * Resolve the prepared arguments stored in the given bean definition.
 */
private Object[] resolvePreparedArguments(String beanName, BeanDefinition merged, Executable executable, Object[] argsToResolve, BeanWrapper bw) {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : bw;
    BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(beanFactory, beanName, merged, converter);
    Class<?>[] paramTypes = executable.getParameterTypes();
    Object[] resolvedArgs = new Object[argsToResolve.length];
    for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
        Object argValue = argsToResolve[argIndex];
        MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
        if (argValue == autowiredArgumentMarker) {
            argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, true);
        } else if (argValue instanceof BeanMetadataElement) {
            argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
        } else if (argValue instanceof String) {
            argValue = beanFactory.evaluateBeanDefinitionString((String) argValue, merged);
        }
        Class<?> paramType = paramTypes[argIndex];
        try {
            resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
        } catch (TypeMismatchException ex) {
            throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
        }
    }
    return resolvedArgs;
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) BeanMetadataElement(cn.taketoday.beans.BeanMetadataElement) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter)

Example 3 with InjectionPoint

use of cn.taketoday.beans.factory.InjectionPoint in project today-framework by TAKETODAY.

the class ConstructorResolver method resolveAutowiredArgument.

/**
 * Template method for resolving the specified argument which is supposed to be autowired.
 */
@Nullable
private Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter, boolean fallback) {
    Class<?> paramType = param.getParameterType();
    if (InjectionPoint.class.isAssignableFrom(paramType)) {
        InjectionPoint injectionPoint = currentInjectionPoint.get();
        if (injectionPoint == null) {
            throw new IllegalStateException("No current InjectionPoint available for " + param);
        }
        return injectionPoint;
    }
    try {
        DependencyResolvingContext context = new DependencyResolvingContext(param.getExecutable(), beanFactory, beanName);
        context.setTypeConverter(typeConverter);
        context.setDependentBeans(autowiredBeanNames);
        return injector.resolveValue(new DependencyDescriptor(param, true), context);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw ex;
    } catch (NoSuchBeanDefinitionException ex) {
        if (fallback) {
            // for e.g. a vararg or a non-null List/Set/Map parameter.
            if (paramType.isArray()) {
                return Array.newInstance(paramType.getComponentType(), 0);
            } else if (CollectionUtils.isApproximableCollectionType(paramType)) {
                return CollectionUtils.createCollection(paramType, 0);
            } else if (CollectionUtils.isApproximableMapType(paramType)) {
                return CollectionUtils.createMap(paramType, 0);
            }
        }
        throw ex;
    }
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Nullable(cn.taketoday.lang.Nullable)

Example 4 with InjectionPoint

use of cn.taketoday.beans.factory.InjectionPoint in project today-framework by TAKETODAY.

the class ConstructorResolver method createArgumentArray.

/**
 * Create an array of arguments to invoke a constructor or factory method,
 * given the resolved constructor argument values.
 */
private ArgumentsHolder createArgumentArray(String beanName, RootBeanDefinition merged, @Nullable ConstructorArgumentValues resolvedValues, Class<?>[] paramTypes, @Nullable String[] paramNames, Executable executable, BeanWrapper wrapper, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : wrapper;
    ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
    HashSet<ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
    LinkedHashSet<String> autowiredBeanNames = new LinkedHashSet<>(4);
    for (int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++) {
        Class<?> paramType = paramTypes[paramIndex];
        String paramName = paramNames != null ? paramNames[paramIndex] : "";
        // Try to find matching constructor argument value, either indexed or generic.
        ValueHolder valueHolder = null;
        if (resolvedValues != null) {
            valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders);
            // it could match after type conversion (for example, String -> int).
            if (valueHolder == null && (!autowiring || paramTypes.length == resolvedValues.getArgumentCount())) {
                valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders);
            }
        }
        if (valueHolder != null) {
            // We found a potential match - let's give it a try.
            // Do not consider the same value definition multiple times!
            usedValueHolders.add(valueHolder);
            Object originalValue = valueHolder.getValue();
            Object convertedValue;
            if (valueHolder.isConverted()) {
                convertedValue = valueHolder.getConvertedValue();
                args.preparedArguments[paramIndex] = convertedValue;
            } else {
                MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
                try {
                    convertedValue = converter.convertIfNecessary(originalValue, paramType, methodParam);
                } catch (TypeMismatchException ex) {
                    throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(valueHolder.getValue()) + "] to required type [" + paramType.getName() + "]: " + ex.getMessage());
                }
                Object sourceHolder = valueHolder.getSource();
                if (sourceHolder instanceof ValueHolder) {
                    Object sourceValue = ((ValueHolder) sourceHolder).getValue();
                    args.resolveNecessary = true;
                    args.preparedArguments[paramIndex] = sourceValue;
                }
            }
            args.arguments[paramIndex] = convertedValue;
            args.rawArguments[paramIndex] = originalValue;
        } else {
            MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
            // have to fail creating an argument array for the given constructor.
            if (!autowiring) {
                throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), "Ambiguous argument values for parameter of type [" + paramType.getName() + "] - did you specify the correct bean references as arguments?");
            }
            try {
                Object autowiredArgument = resolveAutowiredArgument(methodParam, beanName, autowiredBeanNames, converter, fallback);
                args.resolveNecessary = true;
                args.arguments[paramIndex] = autowiredArgument;
                args.rawArguments[paramIndex] = autowiredArgument;
                args.preparedArguments[paramIndex] = autowiredArgumentMarker;
            } catch (BeansException ex) {
                throw new UnsatisfiedDependencyException(merged.getResourceDescription(), beanName, new InjectionPoint(methodParam), ex);
            }
        }
    }
    for (String autowiredBeanName : autowiredBeanNames) {
        beanFactory.registerDependentBean(autowiredBeanName, beanName);
        if (log.isDebugEnabled()) {
            log.debug("Autowiring by type from bean name '{}' via {} to bean named '{}'", beanName, (executable instanceof Constructor ? "constructor" : "factory method"), autowiredBeanName);
        }
    }
    return args;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) Constructor(java.lang.reflect.Constructor) TypeMismatchException(cn.taketoday.beans.TypeMismatchException) ValueHolder(cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) TypeConverter(cn.taketoday.beans.TypeConverter) UnsatisfiedDependencyException(cn.taketoday.beans.factory.UnsatisfiedDependencyException) MethodParameter(cn.taketoday.core.MethodParameter) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) BeansException(cn.taketoday.beans.BeansException)

Example 5 with InjectionPoint

use of cn.taketoday.beans.factory.InjectionPoint in project today-infrastructure by TAKETODAY.

the class ConstructorResolver method resolveAutowiredArgument.

/**
 * Template method for resolving the specified argument which is supposed to be autowired.
 */
@Nullable
private Object resolveAutowiredArgument(MethodParameter param, String beanName, @Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter, boolean fallback) {
    Class<?> paramType = param.getParameterType();
    if (InjectionPoint.class.isAssignableFrom(paramType)) {
        InjectionPoint injectionPoint = currentInjectionPoint.get();
        if (injectionPoint == null) {
            throw new IllegalStateException("No current InjectionPoint available for " + param);
        }
        return injectionPoint;
    }
    try {
        DependencyResolvingContext context = new DependencyResolvingContext(param.getExecutable(), beanFactory, beanName);
        context.setTypeConverter(typeConverter);
        context.setDependentBeans(autowiredBeanNames);
        return injector.resolveValue(new DependencyDescriptor(param, true), context);
    } catch (NoUniqueBeanDefinitionException ex) {
        throw ex;
    } catch (NoSuchBeanDefinitionException ex) {
        if (fallback) {
            // for e.g. a vararg or a non-null List/Set/Map parameter.
            if (paramType.isArray()) {
                return Array.newInstance(paramType.getComponentType(), 0);
            } else if (CollectionUtils.isApproximableCollectionType(paramType)) {
                return CollectionUtils.createCollection(paramType, 0);
            } else if (CollectionUtils.isApproximableMapType(paramType)) {
                return CollectionUtils.createMap(paramType, 0);
            }
        }
        throw ex;
    }
}
Also used : InjectionPoint(cn.taketoday.beans.factory.InjectionPoint) DependencyDescriptor(cn.taketoday.beans.factory.config.DependencyDescriptor) NoSuchBeanDefinitionException(cn.taketoday.beans.factory.NoSuchBeanDefinitionException) NoUniqueBeanDefinitionException(cn.taketoday.beans.factory.NoUniqueBeanDefinitionException) Nullable(cn.taketoday.lang.Nullable)

Aggregations

InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)7 TypeConverter (cn.taketoday.beans.TypeConverter)5 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)5 UnsatisfiedDependencyException (cn.taketoday.beans.factory.UnsatisfiedDependencyException)5 MethodParameter (cn.taketoday.core.MethodParameter)5 BeansException (cn.taketoday.beans.BeansException)3 ValueHolder (cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder)3 Constructor (java.lang.reflect.Constructor)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 BeanMetadataElement (cn.taketoday.beans.BeanMetadataElement)2 NoSuchBeanDefinitionException (cn.taketoday.beans.factory.NoSuchBeanDefinitionException)2 NoUniqueBeanDefinitionException (cn.taketoday.beans.factory.NoUniqueBeanDefinitionException)2 DependencyDescriptor (cn.taketoday.beans.factory.config.DependencyDescriptor)2 Nullable (cn.taketoday.lang.Nullable)2