Search in sources :

Example 21 with TypeDescriptor

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

the class StreamConverterTests method convertFromStreamToRawList.

@Test
void convertFromStreamToRawList() throws NoSuchFieldException {
    Stream<Integer> stream = Stream.of(1, 2, 3);
    TypeDescriptor listOfStrings = new TypeDescriptor(Types.class.getField("rawList"));
    Object result = this.conversionService.convert(stream, listOfStrings);
    assertThat(result).asInstanceOf(list(Object.class)).containsExactly(1, 2, 3);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 22 with TypeDescriptor

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

the class StreamConverterTests method convertFromStreamToArrayNoConverter.

@Test
void convertFromStreamToArrayNoConverter() throws NoSuchFieldException {
    Stream<Integer> stream = Stream.of(1, 2, 3);
    TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
    assertThatExceptionOfType(ConversionFailedException.class).isThrownBy(() -> this.conversionService.convert(stream, arrayOfLongs)).withCauseInstanceOf(ConverterNotFoundException.class);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 23 with TypeDescriptor

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

the class StreamConverterTests method convertFromArrayToStream.

@Test
@SuppressWarnings("resource")
void convertFromArrayToStream() throws NoSuchFieldException {
    Integer[] array = new Integer[] { 1, 0, 1 };
    this.conversionService.addConverter(Integer.class, Boolean.class, source -> source == 1);
    TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans"));
    Object result = this.conversionService.convert(array, streamOfBoolean);
    assertThat(result).asInstanceOf(stream(Boolean.class)).filteredOn(x -> x).hasSize(2);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) InstanceOfAssertFactories.list(org.assertj.core.api.InstanceOfAssertFactories.list) Test(org.junit.jupiter.api.Test) ConversionFailedException(cn.taketoday.core.conversion.ConversionFailedException) List(java.util.List) Stream(java.util.stream.Stream) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) InstanceOfAssertFactories.stream(org.assertj.core.api.InstanceOfAssertFactories.stream) TypeDescriptor(cn.taketoday.core.TypeDescriptor) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 24 with TypeDescriptor

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

the class StreamConverterTests method shouldFailToConvertIfNoStream.

@Test
void shouldFailToConvertIfNoStream() throws NoSuchFieldException {
    TypeDescriptor sourceType = new TypeDescriptor(Types.class.getField("listOfStrings"));
    TypeDescriptor targetType = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
    assertThatIllegalStateException().isThrownBy(() -> this.streamConverter.convert(new Object(), sourceType, targetType));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 25 with TypeDescriptor

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

the class AbstractPropertyBindingResult method findEditor.

/**
 * This implementation exposes a PropertyEditor adapter for a Formatter,
 * if applicable.
 */
@Override
@Nullable
public PropertyEditor findEditor(@Nullable String field, @Nullable Class<?> valueType) {
    Class<?> valueTypeForLookup = valueType;
    if (valueTypeForLookup == null) {
        valueTypeForLookup = getFieldType(field);
    }
    PropertyEditor editor = super.findEditor(field, valueTypeForLookup);
    if (editor == null && this.conversionService != null) {
        TypeDescriptor td = null;
        if (field != null && getTarget() != null) {
            TypeDescriptor ptd = getPropertyAccessor().getPropertyTypeDescriptor(fixedField(field));
            if (ptd != null && (valueType == null || valueType.isAssignableFrom(ptd.getType()))) {
                td = ptd;
            }
        }
        if (td == null) {
            td = TypeDescriptor.valueOf(valueTypeForLookup);
        }
        if (this.conversionService.canConvert(TypeDescriptor.valueOf(String.class), td)) {
            editor = new ConvertingPropertyEditorAdapter(this.conversionService, td);
        }
    }
    return editor;
}
Also used : ConvertingPropertyEditorAdapter(cn.taketoday.core.conversion.support.ConvertingPropertyEditorAdapter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) PropertyEditor(java.beans.PropertyEditor) 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