Search in sources :

Example 1 with ConverterNotFoundException

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);
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) Nullable(cn.taketoday.lang.Nullable)

Example 2 with ConverterNotFoundException

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 3 with ConverterNotFoundException

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 4 with ConverterNotFoundException

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);
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) Nullable(cn.taketoday.lang.Nullable)

Example 5 with ConverterNotFoundException

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) EnumMap(java.util.EnumMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

ConverterNotFoundException (cn.taketoday.core.conversion.ConverterNotFoundException)10 TypeDescriptor (cn.taketoday.core.TypeDescriptor)8 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)8 Test (org.junit.jupiter.api.Test)8 MultiValueMap (cn.taketoday.core.MultiValueMap)6 EnumMap (java.util.EnumMap)6 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 Map (java.util.Map)6 List (java.util.List)4 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)2 Nullable (cn.taketoday.lang.Nullable)2 ArrayList (java.util.ArrayList)2