use of cn.taketoday.core.conversion.ConverterNotFoundException in project today-infrastructure by TAKETODAY.
the class Binder method bindObject.
@Nullable
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, boolean allowRecursiveBinding) {
ConfigurationProperty property = findProperty(name, target, context);
if (property == null && context.depth != 0 && containsNoDescendantOf(context.getSources(), name)) {
return null;
}
AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
if (aggregateBinder != null) {
return bindAggregate(name, target, handler, context, aggregateBinder);
}
if (property != null) {
try {
return bindProperty(target, context, property);
} catch (ConverterNotFoundException ex) {
// We might still be able to bind it using the recursive binders
Object instance = bindDataObject(name, target, handler, context, allowRecursiveBinding);
if (instance != null) {
return instance;
}
throw ex;
}
}
return bindDataObject(name, target, handler, context, allowRecursiveBinding);
}
use of cn.taketoday.core.conversion.ConverterNotFoundException in project today-infrastructure by TAKETODAY.
the class MapToMapConverterTests method scalarMap.
@Test
void scalarMap() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("1", "9");
map.put("2", "37");
TypeDescriptor sourceType = TypeDescriptor.fromObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException ex) {
assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue();
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
@SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
assertThat(map.equals(result)).isFalse();
assertThat((int) result.get(1)).isEqualTo(9);
assertThat((int) result.get(2)).isEqualTo(37);
}
use of cn.taketoday.core.conversion.ConverterNotFoundException in project today-infrastructure by TAKETODAY.
the class MapToMapConverterTests method scalarMapNotGenericSourceField.
@Test
void scalarMapNotGenericSourceField() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("1", "9");
map.put("2", "37");
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException ex) {
assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue();
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
@SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
assertThat(map.equals(result)).isFalse();
assertThat((int) result.get(1)).isEqualTo(9);
assertThat((int) result.get(2)).isEqualTo(37);
}
use of cn.taketoday.core.conversion.ConverterNotFoundException in project today-framework by TAKETODAY.
the class Binder method bindObject.
@Nullable
private <T> Object bindObject(ConfigurationPropertyName name, Bindable<T> target, BindHandler handler, Context context, boolean allowRecursiveBinding) {
ConfigurationProperty property = findProperty(name, target, context);
if (property == null && context.depth != 0 && containsNoDescendantOf(context.getSources(), name)) {
return null;
}
AggregateBinder<?> aggregateBinder = getAggregateBinder(target, context);
if (aggregateBinder != null) {
return bindAggregate(name, target, handler, context, aggregateBinder);
}
if (property != null) {
try {
return bindProperty(target, context, property);
} catch (ConverterNotFoundException ex) {
// We might still be able to bind it using the recursive binders
Object instance = bindDataObject(name, target, handler, context, allowRecursiveBinding);
if (instance != null) {
return instance;
}
throw ex;
}
}
return bindDataObject(name, target, handler, context, allowRecursiveBinding);
}
use of cn.taketoday.core.conversion.ConverterNotFoundException in project today-framework by TAKETODAY.
the class MapToMapConverterTests method scalarMapNotGenericSourceField.
@Test
void scalarMapNotGenericSourceField() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("1", "9");
map.put("2", "37");
TypeDescriptor sourceType = new TypeDescriptor(getClass().getField("notGenericMapSource"));
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
try {
conversionService.convert(map, sourceType, targetType);
} catch (ConversionFailedException ex) {
assertThat(ex.getCause() instanceof ConverterNotFoundException).isTrue();
}
conversionService.addConverterFactory(new StringToNumberConverterFactory());
assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
@SuppressWarnings("unchecked") Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
assertThat(map.equals(result)).isFalse();
assertThat((int) result.get(1)).isEqualTo(9);
assertThat((int) result.get(2)).isEqualTo(37);
}
Aggregations