@Override public void addFormatterForFieldType(Class<?> fieldType, Printer<?> printer, Parser<?> parser) { addConverter(new PrinterConverter(fieldType, printer, this)); addConverter(new ParserConverter(fieldType, parser, this)); }
/** * Determine the {@link Formatter}-declared field type. * @return the field type declared in the wrapped {@link Formatter} implementation * (never {@code null}) * @throws IllegalArgumentException if the {@link Formatter}-declared field type * cannot be inferred */ public Class<?> getFieldType() { return FormattingConversionService.getFieldType(this.formatter); }
@Override public void addFormatter(Formatter<?> formatter) { addFormatterForFieldType(getFieldType(formatter), formatter); }
@Override public void afterPropertiesSet() { this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); registerFormatters(this.conversionService); }
@Test public void testFormatterRegistrar() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<FormatterRegistrar> registrars = new HashSet<>(); registrars.add(new TestFormatterRegistrar()); factory.setFormatterRegistrars(registrars); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TestBean testBean = fcs.convert("5", TestBean.class); assertEquals(5, testBean.getSpecialInt()); assertEquals("5", fcs.convert(testBean, String.class)); }
@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(expected = ConversionFailedException.class) public void parseParserReturnsNull() throws ParseException { formattingService.addFormatterForFieldType(Integer.class, new NullReturningFormatter()); assertNull(formattingService.convert("1", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class))); }
private <T> void registerDefaultValue(Class<T> clazz, final T defaultValue) { formattingService.addFormatterForFieldType(clazz, new Formatter<T>() { @Override public T parse(String text, Locale locale) throws ParseException { return defaultValue; } @Override public String print(T t, Locale locale) { return defaultValue.toString(); } @Override public String toString() { return defaultValue.toString(); } }); }
@Test public void formatFieldForAnnotation() throws Exception { formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(Model.class, false); }
@Override public TestBean parse(String text, Locale locale) throws ParseException { TestBean object = new TestBean(); object.setSpecialInt(Integer.parseInt(text)); return object; } }
@Override public Printer<?> getPrinter(SpecialInt annotation, Class<?> fieldType) { assertEquals("aliased", annotation.value()); assertEquals("aliased", annotation.alias()); return new Printer<Integer>() { @Override public String print(Integer object, Locale locale) { return ":" + object.toString(); } }; }
@Override public Long convert(MyDate source) { return source.getTime(); } });
@Override @SuppressWarnings("unchecked") public <T extends Number> Converter<String, T> getConverter(Class<T> targetType) { if (Integer.class == targetType) { return (Converter<String, T>) new IntegerConverter(); } else { throw new IllegalStateException(); } } }
@Override public void registerFormatters(FormatterRegistry registry) { registry.addFormatter(new TestBeanFormatter()); } }
private static class TestBean { @NumberFormat(pattern = "##,00") private double pattern; @SpecialInt("aliased") private int specialInt; public int getSpecialInt() { return specialInt; } public void setSpecialInt(int field) { this.specialInt = field; } }
@Override public void addFormatterForFieldType(Class<?> fieldType, Formatter<?> formatter) { addConverter(new PrinterConverter(fieldType, formatter, this)); addConverter(new ParserConverter(fieldType, formatter, this)); }
@Test public void formatFieldForAnnotationWithDirectFieldAccess() throws Exception { formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(Model.class, true); }
@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)); }