use of cn.taketoday.beans.TypeConverter 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;
}
use of cn.taketoday.beans.TypeConverter in project today-infrastructure by TAKETODAY.
the class ArgumentConvertingMethodInvoker method registerCustomEditor.
/**
* Register the given custom property editor for all properties of the given type.
* <p>Typically used in conjunction with the default
* {@link cn.taketoday.beans.SimpleTypeConverter}; will work with any
* TypeConverter that implements the PropertyEditorRegistry interface as well.
*
* @param requiredType type of the property
* @param propertyEditor editor to register
* @see #setTypeConverter
* @see cn.taketoday.beans.PropertyEditorRegistry#registerCustomEditor
*/
public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) {
TypeConverter converter = getTypeConverter();
if (!(converter instanceof PropertyEditorRegistry)) {
throw new IllegalStateException("TypeConverter does not implement PropertyEditorRegistry interface: " + converter);
}
((PropertyEditorRegistry) converter).registerCustomEditor(requiredType, propertyEditor);
}
use of cn.taketoday.beans.TypeConverter in project today-framework 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-framework 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-framework 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;
}
Aggregations