use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class StreamConverterTests method convertFromArrayToStream.
@Test
@SuppressWarnings("resource")
public void convertFromArrayToStream() throws NoSuchFieldException {
Integer[] stream = new Integer[] { 1, 0, 1 };
this.conversionService.addConverter(new Converter<Integer, Boolean>() {
@Override
public Boolean convert(Integer source) {
return source == 1;
}
});
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans"));
;
Object result = this.conversionService.convert(stream, streamOfBoolean);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a stream", result instanceof Stream);
@SuppressWarnings("unchecked") Stream<Boolean> content = (Stream<Boolean>) result;
assertEquals(2, content.filter(x -> x).count());
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class StreamConverterTests method convertFromStreamToArrayNoConverter.
@Test
public void convertFromStreamToArrayNoConverter() throws NoSuchFieldException {
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
;
thrown.expect(ConversionFailedException.class);
thrown.expectCause(is(instanceOf(ConverterNotFoundException.class)));
this.conversionService.convert(stream, arrayOfLongs);
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class StreamConverterTests method convertFromStreamToArray.
@Test
public void convertFromStreamToArray() throws NoSuchFieldException {
this.conversionService.addConverterFactory(new NumberToNumberConverterFactory());
Stream<Integer> stream = Arrays.asList(1, 2, 3).stream();
TypeDescriptor arrayOfLongs = new TypeDescriptor(Types.class.getField("arrayOfLongs"));
;
Object result = this.conversionService.convert(stream, arrayOfLongs);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be an array", result.getClass().isArray());
Long[] content = (Long[]) result;
assertEquals(Long.valueOf(1L), content[0]);
assertEquals(Long.valueOf(2L), content[1]);
assertEquals(Long.valueOf(3L), content[2]);
assertEquals("Wrong number of elements", 3, content.length);
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class StreamConverterTests method convertFromListToRawStream.
@Test
@SuppressWarnings("resource")
public void convertFromListToRawStream() throws NoSuchFieldException {
List<String> stream = Arrays.asList("1", "2", "3");
TypeDescriptor streamOfInteger = new TypeDescriptor(Types.class.getField("rawStream"));
;
Object result = this.conversionService.convert(stream, streamOfInteger);
assertNotNull("Converted object must not be null", result);
assertTrue("Converted object must be a stream", result instanceof Stream);
@SuppressWarnings("unchecked") Stream<Object> content = (Stream<Object>) result;
StringBuilder sb = new StringBuilder();
content.forEach(sb::append);
assertEquals("123", sb.toString());
}
use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.
the class AbstractPropertyBindingResult method formatFieldValue.
/**
* Formats the field value based on registered PropertyEditors.
* @see #getCustomEditor
*/
@Override
protected Object formatFieldValue(String field, Object value) {
String fixedField = fixedField(field);
// Try custom editor...
PropertyEditor customEditor = getCustomEditor(fixedField);
if (customEditor != null) {
customEditor.setValue(value);
String textValue = customEditor.getAsText();
// text representation for this value: only use it if non-null.
if (textValue != null) {
return textValue;
}
}
if (this.conversionService != null) {
// Try custom converter...
TypeDescriptor fieldDesc = getPropertyAccessor().getPropertyTypeDescriptor(fixedField);
TypeDescriptor strDesc = TypeDescriptor.valueOf(String.class);
if (fieldDesc != null && this.conversionService.canConvert(fieldDesc, strDesc)) {
return this.conversionService.convert(value, fieldDesc, strDesc);
}
}
return value;
}
Aggregations