Search in sources :

Example 26 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class NumberToDataSizeConverterTests method convert.

@SuppressWarnings({ "rawtypes", "unchecked" })
private DataSize convert(ConversionService conversionService, Integer source, DataUnit defaultUnit) {
    TypeDescriptor targetType = mock(TypeDescriptor.class);
    if (defaultUnit != null) {
        DataSizeUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", defaultUnit), DataSizeUnit.class, null);
        given(targetType.getAnnotation(DataSizeUnit.class)).willReturn(unitAnnotation);
    }
    given(targetType.getType()).willReturn((Class) DataSize.class);
    return (DataSize) conversionService.convert(source, TypeDescriptor.fromObject(source), targetType);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) DataSize(cn.taketoday.util.DataSize) DataSizeUnit(cn.taketoday.format.annotation.DataSizeUnit)

Example 27 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class MockPeriodTypeDescriptor method get.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static TypeDescriptor get(ChronoUnit unit, PeriodStyle style) {
    TypeDescriptor descriptor = mock(TypeDescriptor.class);
    if (unit != null) {
        PeriodUnit unitAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", unit), PeriodUnit.class, null);
        given(descriptor.getAnnotation(PeriodUnit.class)).willReturn(unitAnnotation);
    }
    if (style != null) {
        PeriodFormat formatAnnotation = AnnotationUtils.synthesizeAnnotation(Collections.singletonMap("value", style), PeriodFormat.class, null);
        given(descriptor.getAnnotation(PeriodFormat.class)).willReturn(formatAnnotation);
    }
    given(descriptor.getType()).willReturn((Class) Period.class);
    given(descriptor.getObjectType()).willReturn((Class) Period.class);
    return descriptor;
}
Also used : PeriodFormat(cn.taketoday.format.annotation.PeriodFormat) TypeDescriptor(cn.taketoday.core.TypeDescriptor) Period(java.time.Period) PeriodUnit(cn.taketoday.format.annotation.PeriodUnit)

Example 28 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class CollectionToCollectionConverter method convert.

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source == null) {
        return null;
    }
    Collection<?> sourceCollection = (Collection<?>) source;
    // Shortcut if possible...
    boolean copyRequired = !targetType.getType().isInstance(source);
    if (!copyRequired && sourceCollection.isEmpty()) {
        return source;
    }
    TypeDescriptor elementDesc = targetType.getElementDescriptor();
    if (elementDesc == null && !copyRequired) {
        return source;
    }
    // At this point, we need a collection copy in any case, even if just for finding out about element copies...
    Collection<Object> target = CollectionUtils.createCollection(targetType.getType(), (elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());
    if (elementDesc == null) {
        target.addAll(sourceCollection);
    } else {
        for (Object sourceElement : sourceCollection) {
            Object targetElement = conversionService.convert(sourceElement, sourceType.elementDescriptor(sourceElement), elementDesc);
            target.add(targetElement);
            if (sourceElement != targetElement) {
                copyRequired = true;
            }
        }
    }
    return (copyRequired ? target : source);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Collection(java.util.Collection) Nullable(cn.taketoday.lang.Nullable)

Example 29 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class StringToArrayConverter method convert.

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source == null) {
        return null;
    }
    String string = (String) source;
    String[] fields = StringUtils.commaDelimitedListToStringArray(string);
    TypeDescriptor targetElementType = targetType.getElementDescriptor();
    Assert.state(targetElementType != null, "No target element type");
    Object target = Array.newInstance(targetElementType.getType(), fields.length);
    for (int i = 0; i < fields.length; i++) {
        String sourceElement = fields[i];
        Object targetElement = conversionService.convert(sourceElement.trim(), sourceType, targetElementType);
        Array.set(target, i, targetElement);
    }
    return target;
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Nullable(cn.taketoday.lang.Nullable)

Example 30 with TypeDescriptor

use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.

the class StreamConverter method convertFromStream.

@Nullable
private Object convertFromStream(@Nullable Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) {
    List<Object> content = source != null ? source.collect(Collectors.<Object>toList()) : Collections.emptyList();
    TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementDescriptor());
    return conversionService.convert(content, listType, targetType);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Nullable(cn.taketoday.lang.Nullable)

Aggregations

TypeDescriptor (cn.taketoday.core.TypeDescriptor)221 Test (org.junit.jupiter.api.Test)128 ArrayList (java.util.ArrayList)44 Nullable (cn.taketoday.lang.Nullable)36 List (java.util.List)28 LinkedHashMap (java.util.LinkedHashMap)20 MethodParameter (cn.taketoday.core.MethodParameter)18 ConverterNotFoundException (cn.taketoday.core.conversion.ConverterNotFoundException)18 HashMap (java.util.HashMap)18 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)16 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)16 MultiValueMap (cn.taketoday.core.MultiValueMap)14 EnumMap (java.util.EnumMap)12 MethodExecutor (cn.taketoday.expression.MethodExecutor)10 Collection (java.util.Collection)10 Map (java.util.Map)10 ConversionService (cn.taketoday.core.conversion.ConversionService)8 ClassPathResource (cn.taketoday.core.io.ClassPathResource)8 AccessException (cn.taketoday.expression.AccessException)8 EvaluationException (cn.taketoday.expression.EvaluationException)8