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