Search in sources :

Example 31 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class SpelReproTests method projectionTypeDescriptors_3.

@Test
public void projectionTypeDescriptors_3() throws Exception {
    StandardEvaluationContext ctx = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "ms.![key.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    List<?> value = (List<?>) exp.getValue(ctx);
    // value is list containing [true,false]
    assertEquals(Boolean.class, value.get(0).getClass());
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(ctx);
    assertEquals(null, evaluated.getElementTypeDescriptor());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 32 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class FormattingConversionServiceTests method doTestFormatFieldForAnnotation.

@SuppressWarnings("unchecked")
private void doTestFormatFieldForAnnotation(Class<?> modelClass, boolean directFieldAccess) throws Exception {
    formattingService.addConverter(new Converter<Date, Long>() {

        @Override
        public Long convert(Date source) {
            return source.getTime();
        }
    });
    formattingService.addConverter(new Converter<DateTime, Date>() {

        @Override
        public Date convert(DateTime source) {
            return source.toDate();
        }
    });
    String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime().toDate(), new TypeDescriptor(modelClass.getField("date")), TypeDescriptor.valueOf(String.class));
    assertEquals("10/31/09", formatted);
    LocalDate date = new LocalDate(formattingService.convert("10/31/09", TypeDescriptor.valueOf(String.class), new TypeDescriptor(modelClass.getField("date"))));
    assertEquals(new LocalDate(2009, 10, 31), date);
    List<Date> dates = new ArrayList<>();
    dates.add(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime().toDate());
    dates.add(new LocalDate(2009, 11, 1).toDateTimeAtCurrentTime().toDate());
    dates.add(new LocalDate(2009, 11, 2).toDateTimeAtCurrentTime().toDate());
    formatted = (String) formattingService.convert(dates, new TypeDescriptor(modelClass.getField("dates")), TypeDescriptor.valueOf(String.class));
    assertEquals("10-31-09,11-1-09,11-2-09", formatted);
    dates = (List<Date>) formattingService.convert("10-31-09,11-1-09,11-2-09", TypeDescriptor.valueOf(String.class), new TypeDescriptor(modelClass.getField("dates")));
    assertEquals(new LocalDate(2009, 10, 31), new LocalDate(dates.get(0)));
    assertEquals(new LocalDate(2009, 11, 1), new LocalDate(dates.get(1)));
    assertEquals(new LocalDate(2009, 11, 2), new LocalDate(dates.get(2)));
    Object model = modelClass.newInstance();
    ConfigurablePropertyAccessor accessor = directFieldAccess ? PropertyAccessorFactory.forDirectFieldAccess(model) : PropertyAccessorFactory.forBeanPropertyAccess(model);
    accessor.setConversionService(formattingService);
    accessor.setPropertyValue("dates", "10-31-09,11-1-09,11-2-09");
    dates = (List<Date>) accessor.getPropertyValue("dates");
    assertEquals(new LocalDate(2009, 10, 31), new LocalDate(dates.get(0)));
    assertEquals(new LocalDate(2009, 11, 1), new LocalDate(dates.get(1)));
    assertEquals(new LocalDate(2009, 11, 2), new LocalDate(dates.get(2)));
    if (!directFieldAccess) {
        accessor.setPropertyValue("dates[0]", "10-30-09");
        accessor.setPropertyValue("dates[1]", "10-1-09");
        accessor.setPropertyValue("dates[2]", "10-2-09");
        dates = (List<Date>) accessor.getPropertyValue("dates");
        assertEquals(new LocalDate(2009, 10, 30), new LocalDate(dates.get(0)));
        assertEquals(new LocalDate(2009, 10, 1), new LocalDate(dates.get(1)));
        assertEquals(new LocalDate(2009, 10, 2), new LocalDate(dates.get(2)));
    }
}
Also used : ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConfigurablePropertyAccessor(org.springframework.beans.ConfigurablePropertyAccessor)

Example 33 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class FormattingConversionServiceTests method formatFieldForAnnotationWithSubclassAsFieldType.

@Test
public void formatFieldForAnnotationWithSubclassAsFieldType() throws Exception {
    formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory() {

        @Override
        public Printer<?> getPrinter(org.springframework.format.annotation.DateTimeFormat annotation, Class<?> fieldType) {
            assertEquals(MyDate.class, fieldType);
            return super.getPrinter(annotation, fieldType);
        }
    });
    formattingService.addConverter(new Converter<MyDate, Long>() {

        @Override
        public Long convert(MyDate source) {
            return source.getTime();
        }
    });
    formattingService.addConverter(new Converter<MyDate, Date>() {

        @Override
        public Date convert(MyDate source) {
            return source;
        }
    });
    formattingService.convert(new MyDate(), new TypeDescriptor(ModelWithSubclassField.class.getField("date")), TypeDescriptor.valueOf(String.class));
}
Also used : Printer(org.springframework.format.Printer) ReadablePartialPrinter(org.springframework.format.datetime.joda.ReadablePartialPrinter) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) JodaDateTimeFormatAnnotationFormatterFactory(org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory) Test(org.junit.Test)

Example 34 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class MapToMapConverter method convert.

@Override
@SuppressWarnings("unchecked")
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source == null) {
        return null;
    }
    Map<Object, Object> sourceMap = (Map<Object, Object>) source;
    // Shortcut if possible...
    boolean copyRequired = !targetType.getType().isInstance(source);
    if (!copyRequired && sourceMap.isEmpty()) {
        return sourceMap;
    }
    TypeDescriptor keyDesc = targetType.getMapKeyTypeDescriptor();
    TypeDescriptor valueDesc = targetType.getMapValueTypeDescriptor();
    List<MapEntry> targetEntries = new ArrayList<>(sourceMap.size());
    for (Map.Entry<Object, Object> entry : sourceMap.entrySet()) {
        Object sourceKey = entry.getKey();
        Object sourceValue = entry.getValue();
        Object targetKey = convertKey(sourceKey, sourceType, keyDesc);
        Object targetValue = convertValue(sourceValue, sourceType, valueDesc);
        targetEntries.add(new MapEntry(targetKey, targetValue));
        if (sourceKey != targetKey || sourceValue != targetValue) {
            copyRequired = true;
        }
    }
    if (!copyRequired) {
        return sourceMap;
    }
    Map<Object, Object> targetMap = CollectionFactory.createMap(targetType.getType(), (keyDesc != null ? keyDesc.getType() : null), sourceMap.size());
    for (MapEntry entry : targetEntries) {
        entry.addToMap(targetMap);
    }
    return targetMap;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 35 with TypeDescriptor

use of org.springframework.core.convert.TypeDescriptor in project spring-framework by spring-projects.

the class ObjectToCollectionConverter method convert.

@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
    if (source == null) {
        return null;
    }
    TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
    Collection<Object> target = CollectionFactory.createCollection(targetType.getType(), (elementDesc != null ? elementDesc.getType() : null), 1);
    if (elementDesc == null || elementDesc.isCollection()) {
        target.add(source);
    } else {
        Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
        target.add(singleElement);
    }
    return target;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor)

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