Search in sources :

Example 11 with TypeConverter

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

the class MapFactoryBean method createBeanInstance.

@Override
@SuppressWarnings("unchecked")
protected Map<Object, Object> createBeanInstance() {
    if (this.sourceMap == null) {
        throw new IllegalArgumentException("'sourceMap' is required");
    }
    Map<Object, Object> result = null;
    if (this.targetMapClass != null) {
        result = BeanUtils.newInstance(this.targetMapClass);
    } else {
        result = CollectionUtils.newLinkedHashMap(this.sourceMap.size());
    }
    Class<?> keyType = null;
    Class<?> valueType = null;
    if (this.targetMapClass != null) {
        ResolvableType mapType = ResolvableType.fromClass(this.targetMapClass).asMap();
        keyType = mapType.resolveGeneric(0);
        valueType = mapType.resolveGeneric(1);
    }
    if (keyType != null || valueType != null) {
        TypeConverter converter = getBeanTypeConverter();
        for (Map.Entry<?, ?> entry : this.sourceMap.entrySet()) {
            Object convertedKey = converter.convertIfNecessary(entry.getKey(), keyType);
            Object convertedValue = converter.convertIfNecessary(entry.getValue(), valueType);
            result.put(convertedKey, convertedValue);
        }
    } else {
        result.putAll(this.sourceMap);
    }
    return result;
}
Also used : TypeConverter(cn.taketoday.beans.TypeConverter) ResolvableType(cn.taketoday.core.ResolvableType) Map(java.util.Map)

Example 12 with TypeConverter

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

the class SetFactoryBean method createBeanInstance.

@Override
@SuppressWarnings("unchecked")
protected Set<Object> createBeanInstance() {
    if (this.sourceSet == null) {
        throw new IllegalArgumentException("'sourceSet' is required");
    }
    Set<Object> result;
    if (this.targetSetClass != null) {
        result = BeanUtils.newInstance(this.targetSetClass);
    } else {
        result = new LinkedHashSet<>(this.sourceSet.size());
    }
    Class<?> valueType = null;
    if (this.targetSetClass != null) {
        valueType = ResolvableType.fromClass(this.targetSetClass).asCollection().resolveGeneric();
    }
    if (valueType != null) {
        TypeConverter converter = getBeanTypeConverter();
        for (Object elem : this.sourceSet) {
            result.add(converter.convertIfNecessary(elem, valueType));
        }
    } else {
        result.addAll(this.sourceSet);
    }
    return result;
}
Also used : TypeConverter(cn.taketoday.beans.TypeConverter)

Example 13 with TypeConverter

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

the class ListFactoryBean method createBeanInstance.

@Override
@SuppressWarnings("unchecked")
protected List<Object> createBeanInstance() {
    if (this.sourceList == null) {
        throw new IllegalArgumentException("'sourceList' is required");
    }
    List<Object> result;
    if (this.targetListClass != null) {
        result = BeanUtils.newInstance(this.targetListClass);
    } else {
        result = new ArrayList<>(this.sourceList.size());
    }
    Class<?> valueType = null;
    if (this.targetListClass != null) {
        valueType = ResolvableType.fromClass(this.targetListClass).asCollection().resolveGeneric();
    }
    if (valueType != null) {
        TypeConverter converter = getBeanTypeConverter();
        for (Object elem : this.sourceList) {
            result.add(converter.convertIfNecessary(elem, valueType));
        }
    } else {
        result.addAll(this.sourceList);
    }
    return result;
}
Also used : TypeConverter(cn.taketoday.beans.TypeConverter)

Example 14 with TypeConverter

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

the class ConstructorResolver method resolveConstructorArguments.

/**
 * Resolve the constructor arguments for this bean into the resolvedValues object.
 * This may involve looking up other beans.
 * <p>This method is also used for handling invocations of static factory methods.
 */
private int resolveConstructorArguments(String beanName, BeanDefinition merged, BeanWrapper bw, ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {
    TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
    TypeConverter converter = customConverter != null ? customConverter : bw;
    var valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, merged, converter);
    int minNrOfArgs = cargs.getArgumentCount();
    for (Map.Entry<Integer, ValueHolder> entry : cargs.getIndexedArgumentValues().entrySet()) {
        int index = entry.getKey();
        if (index < 0) {
            throw new BeanCreationException(merged.getResourceDescription(), beanName, "Invalid constructor argument index: " + index);
        }
        if (index + 1 > minNrOfArgs) {
            minNrOfArgs = index + 1;
        }
        ValueHolder valueHolder = entry.getValue();
        if (valueHolder.isConverted()) {
            resolvedValues.addIndexedArgumentValue(index, valueHolder);
        } else {
            Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument", valueHolder.getValue());
            ValueHolder resolvedValueHolder = new ValueHolder(resolvedValue, valueHolder.getType(), valueHolder.getName());
            resolvedValueHolder.setSource(valueHolder);
            resolvedValues.addIndexedArgumentValue(index, resolvedValueHolder);
        }
    }
    for (ValueHolder valueHolder : cargs.getGenericArgumentValues()) {
        if (valueHolder.isConverted()) {
            resolvedValues.addGenericArgumentValue(valueHolder);
        } else {
            Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument", valueHolder.getValue());
            ValueHolder resolvedValueHolder = new ValueHolder(resolvedValue, valueHolder.getType(), valueHolder.getName());
            resolvedValueHolder.setSource(valueHolder);
            resolvedValues.addGenericArgumentValue(resolvedValueHolder);
        }
    }
    return minNrOfArgs;
}
Also used : TypeConverter(cn.taketoday.beans.TypeConverter) BeanCreationException(cn.taketoday.beans.factory.BeanCreationException) ValueHolder(cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder) Map(java.util.Map) InjectionPoint(cn.taketoday.beans.factory.InjectionPoint)

Example 15 with TypeConverter

use of cn.taketoday.beans.TypeConverter in project today-infrastructure 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)

Aggregations

TypeConverter (cn.taketoday.beans.TypeConverter)18 InjectionPoint (cn.taketoday.beans.factory.InjectionPoint)8 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)7 ValueHolder (cn.taketoday.beans.factory.config.ConstructorArgumentValues.ValueHolder)6 UnsatisfiedDependencyException (cn.taketoday.beans.factory.UnsatisfiedDependencyException)5 MethodParameter (cn.taketoday.core.MethodParameter)5 Map (java.util.Map)5 SimpleTypeConverter (cn.taketoday.beans.SimpleTypeConverter)4 BeansException (cn.taketoday.beans.BeansException)3 BeanCreationException (cn.taketoday.beans.factory.BeanCreationException)3 Constructor (java.lang.reflect.Constructor)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 BeanMetadataElement (cn.taketoday.beans.BeanMetadataElement)2 PropertyEditorRegistry (cn.taketoday.beans.PropertyEditorRegistry)2 ResolvableType (cn.taketoday.core.ResolvableType)2 Nullable (cn.taketoday.lang.Nullable)2 Method (java.lang.reflect.Method)2