Search in sources :

Example 51 with TypeDescriptor

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());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Stream(java.util.stream.Stream) Test(org.junit.Test)

Example 52 with TypeDescriptor

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);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Test(org.junit.Test)

Example 53 with TypeDescriptor

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);
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Test(org.junit.Test)

Example 54 with TypeDescriptor

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());
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Stream(java.util.stream.Stream) Test(org.junit.Test)

Example 55 with TypeDescriptor

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;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) PropertyEditor(java.beans.PropertyEditor)

Aggregations

TypeDescriptor (org.springframework.core.convert.TypeDescriptor)115 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)35 List (java.util.List)20 Map (java.util.Map)16 HashMap (java.util.HashMap)14 LinkedHashMap (java.util.LinkedHashMap)13 LinkedList (java.util.LinkedList)12 MethodParameter (org.springframework.core.MethodParameter)12 Collection (java.util.Collection)11 ConversionFailedException (org.springframework.core.convert.ConversionFailedException)10 Method (java.lang.reflect.Method)9 AccessException (org.springframework.expression.AccessException)9 ConverterNotFoundException (org.springframework.core.convert.ConverterNotFoundException)8 TypedValue (org.springframework.expression.TypedValue)8 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)8 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)8 MultiValueMap (org.springframework.util.MultiValueMap)8 AbstractList (java.util.AbstractList)7 Field (java.lang.reflect.Field)6