@Override public void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter) { addConverter(new PrinterConverter(fieldType, formatter, this)); addConverter(new ParserConverter(fieldType, formatter, this)); }
@Override public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) { addConverter(new PrinterConverter(fieldType, printer, this)); addConverter(new ParserConverter(fieldType, parser, this)); }
@Override public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) { addConverter(new PrinterConverter(fieldType, printer, this)); addConverter(new ParserConverter(fieldType, parser, this)); }
@Override public void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter) { addConverter(new PrinterConverter(fieldType, formatter, this)); addConverter(new ParserConverter(fieldType, formatter, this)); }
@Override public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) { Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory); if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver); } Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes(); for (Class<?> fieldType : fieldTypes) { addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType)); addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType)); } }
@Override public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) { Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory); if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver); } Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes(); for (Class<?> fieldType : fieldTypes) { addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType)); addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType)); } }
@Test public void introspectedConverter() { formattingService.addConverter(new IntegerConverter()); assertEquals(Integer.valueOf(1), formattingService.convert("1", Integer.class)); }
@Test public void proxiedConverter() { Converter<?, ?> converter = new IntegerConverter(); formattingService.addConverter((Converter<?, ?>) new ProxyFactory(converter).getProxy()); assertEquals(Integer.valueOf(1), formattingService.convert("1", Integer.class)); }
@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)); }
@SuppressWarnings("unchecked") private void doTestFormatFieldForAnnotation(Class<?> modelClass, boolean directFieldAccess) throws Exception { formattingService.addConverter(new Converter<Date, Long>() { @Override public Long convert(Date source) { formattingService.addConverter(new Converter<DateTime, Date>() { @Override public Date convert(DateTime source) {
@Test public void formatFieldForTypeWithPrinterParserWithCoercion() throws ParseException { formattingService.addConverter(new Converter<DateTime, LocalDate>() { @Override public LocalDate convert(DateTime source) { return source.toLocalDate(); } }); formattingService.addFormatterForFieldType(LocalDate.class, new ReadablePartialPrinter(DateTimeFormat .shortDate()), new DateTimeParser(DateTimeFormat.shortDate())); String formatted = formattingService.convert(new LocalDate(2009, 10, 31), String.class); assertEquals("10/31/09", formatted); LocalDate date = formattingService.convert("10/31/09", LocalDate.class); assertEquals(new LocalDate(2009, 10, 31), date); }
@Test public void addAttributeCustomType() { String attrName = "person"; this.redirectAttributes.addAttribute(attrName, new TestBean("Fred")); assertEquals("ConversionService should have invoked toString()", "Fred", this.redirectAttributes.get(attrName)); this.conversionService.addConverter(new TestBeanConverter()); this.redirectAttributes.addAttribute(attrName, new TestBean("Fred")); assertEquals("Type converter should have been used", "[Fred]", this.redirectAttributes.get(attrName)); }
@Configuration public class MyConfig extends WebMvcConfigurationSupport { @Override public FormattingConversionService mvcConversionService() { FormattingConversionService f = super.mvcConversionService(); f.addConverter(new MyCustomEnumConverter()); return f; } }
@Configuration public class TestConfig extends WebMvcConfigurationSupport { @Override public FormattingConversionService mvcConversionService() { FormattingConversionService f = super.mvcConversionService(); f.addConverter(new StringToStreamConverter()); f.addConverter(new MultipartFileToStreamConverter()); return f; } }
/** * Converter for String -> Optional and Optional -> String */ private Formatters registerOptional() { conversion.addConverter(new GenericConverter() { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.getObjectType().equals(String.class)) { // From String to Optional Object element = conversion.convert(source, sourceType, targetType.elementTypeDescriptor(source)); return Optional.ofNullable(element); } else if (targetType.getObjectType().equals(String.class)) { // From Optional to String if (source == null) return ""; Optional<?> opt = (Optional) source; return opt.map(o -> conversion.convert(source, sourceType.getElementTypeDescriptor(), targetType)) .orElse(""); } return null; } public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() { Set<ConvertiblePair> result = new HashSet<>(); result.add(new ConvertiblePair(Optional.class, String.class)); result.add(new ConvertiblePair(String.class, Optional.class)); return result; } }); return this; }
/** * Converter for String -> Optional and Optional -> String */ private Formatters registerOptional() { conversion.addConverter(new GenericConverter() { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.getObjectType().equals(String.class)) { // From String to Optional Object element = conversion.convert(source, sourceType, targetType.elementTypeDescriptor(source)); return Optional.ofNullable(element); } else if (targetType.getObjectType().equals(String.class)) { // From Optional to String if (source == null) return ""; Optional<?> opt = (Optional) source; return opt.map(o -> conversion.convert(source, sourceType.getElementTypeDescriptor(), targetType)) .orElse(""); } return null; } public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() { Set<ConvertiblePair> result = new HashSet<>(); result.add(new ConvertiblePair(Optional.class, String.class)); result.add(new ConvertiblePair(String.class, Optional.class)); return result; } }); return this; }
@Override public void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter) { addConverter(new PrinterConverter(fieldType, formatter, this)); addConverter(new ParserConverter(fieldType, formatter, this)); }
@Override public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) { addConverter(new PrinterConverter(fieldType, printer, this)); addConverter(new ParserConverter(fieldType, parser, this)); }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { FormattingConversionService service = (FormattingConversionService) beanFactory .getBean("conversionServiceSupport"); beanFactory.getBeansOfType(Converter.class, true, false).forEach((beanName, converter) -> { service.addConverter(converter); }); }
@Override public void addFormatterForFieldAnnotation(AnnotationFormatterFactory<? extends Annotation> annotationFormatterFactory) { Class<? extends Annotation> annotationType = getAnnotationType(annotationFormatterFactory); if (this.embeddedValueResolver != null && annotationFormatterFactory instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) annotationFormatterFactory).setEmbeddedValueResolver(this.embeddedValueResolver); } Set<Class<?>> fieldTypes = annotationFormatterFactory.getFieldTypes(); for (Class<?> fieldType : fieldTypes) { addConverter(new AnnotationPrinterConverter(annotationType, annotationFormatterFactory, fieldType)); addConverter(new AnnotationParserConverter(annotationType, annotationFormatterFactory, fieldType)); } }