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);
}
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);
}
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);
}
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));
}
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;
}
Aggregations