fail when @DateTimeFormat is being used without JodaTime on the classpath (SPR-6508)
This commit is contained in:
parent
61f23710eb
commit
1d005e12af
|
|
@ -46,13 +46,22 @@ import org.springframework.util.StringUtils;
|
|||
* @since 3.0
|
||||
* @see DateTimeFormat
|
||||
*/
|
||||
public final class DateTimeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||
public final class JodaDateTimeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||
|
||||
private final Set<Class<?>> fieldTypes;
|
||||
|
||||
|
||||
public DateTimeFormatAnnotationFormatterFactory() {
|
||||
this.fieldTypes = Collections.unmodifiableSet(createFieldTypes());
|
||||
public JodaDateTimeFormatAnnotationFormatterFactory() {
|
||||
Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(8);
|
||||
rawFieldTypes.add(LocalDate.class);
|
||||
rawFieldTypes.add(LocalTime.class);
|
||||
rawFieldTypes.add(LocalDateTime.class);
|
||||
rawFieldTypes.add(DateTime.class);
|
||||
rawFieldTypes.add(DateMidnight.class);
|
||||
rawFieldTypes.add(Date.class);
|
||||
rawFieldTypes.add(Calendar.class);
|
||||
rawFieldTypes.add(Long.class);
|
||||
this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes);
|
||||
}
|
||||
|
||||
public Set<Class<?>> getFieldTypes() {
|
||||
|
|
@ -85,19 +94,6 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio
|
|||
|
||||
// internal helpers
|
||||
|
||||
private Set<Class<?>> createFieldTypes() {
|
||||
Set<Class<?>> fieldTypes = new HashSet<Class<?>>(8);
|
||||
fieldTypes.add(LocalDate.class);
|
||||
fieldTypes.add(LocalTime.class);
|
||||
fieldTypes.add(LocalDateTime.class);
|
||||
fieldTypes.add(DateTime.class);
|
||||
fieldTypes.add(DateMidnight.class);
|
||||
fieldTypes.add(Date.class);
|
||||
fieldTypes.add(Calendar.class);
|
||||
fieldTypes.add(Long.class);
|
||||
return fieldTypes;
|
||||
}
|
||||
|
||||
private DateTimeFormatter configureDateTimeFormatterFrom(DateTimeFormat annotation) {
|
||||
if (StringUtils.hasLength(annotation.pattern())) {
|
||||
return forPattern(annotation.pattern());
|
||||
|
|
@ -112,7 +112,7 @@ public class JodaTimeFormattingConfigurer {
|
|||
Printer<ReadableInstant> readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter);
|
||||
formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser);
|
||||
|
||||
formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
|
||||
formatterRegistry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -16,10 +16,20 @@
|
|||
|
||||
package org.springframework.format.support;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.format.AnnotationFormatterFactory;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.format.Parser;
|
||||
import org.springframework.format.Printer;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.datetime.joda.JodaTimeFormattingConfigurer;
|
||||
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
|
@ -77,6 +87,40 @@ public class FormattingConversionServiceFactoryBean
|
|||
if (jodaTimePresent) {
|
||||
new JodaTimeFormattingConfigurer().installJodaTimeFormatting(registry);
|
||||
}
|
||||
else {
|
||||
registry.addFormatterForFieldAnnotation(new NoJodaDateTimeFormatAnnotationFormatterFactory());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Dummy AnnotationFormatterFactory that simply fails if @DateTimeFormat is being used
|
||||
* without the JodaTime library being present.
|
||||
*/
|
||||
private static final class NoJodaDateTimeFormatAnnotationFormatterFactory
|
||||
implements AnnotationFormatterFactory<DateTimeFormat> {
|
||||
|
||||
private final Set<Class<?>> fieldTypes;
|
||||
|
||||
public NoJodaDateTimeFormatAnnotationFormatterFactory() {
|
||||
Set<Class<?>> rawFieldTypes = new HashSet<Class<?>>(4);
|
||||
rawFieldTypes.add(Date.class);
|
||||
rawFieldTypes.add(Calendar.class);
|
||||
rawFieldTypes.add(Long.class);
|
||||
this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes);
|
||||
}
|
||||
|
||||
public Set<Class<?>> getFieldTypes() {
|
||||
return this.fieldTypes;
|
||||
}
|
||||
|
||||
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
throw new IllegalStateException("JodaTime library not available - @DateTimeFormat not supported");
|
||||
}
|
||||
|
||||
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
|
||||
throw new IllegalStateException("JodaTime library not available - @DateTimeFormat not supported");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
|||
import org.springframework.core.convert.TypeDescriptor;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.format.datetime.joda.DateTimeFormatAnnotationFormatterFactory;
|
||||
import org.springframework.format.datetime.joda.JodaDateTimeFormatAnnotationFormatterFactory;
|
||||
import org.springframework.format.datetime.joda.DateTimeParser;
|
||||
import org.springframework.format.datetime.joda.ReadablePartialPrinter;
|
||||
import org.springframework.format.number.NumberFormatter;
|
||||
|
|
@ -93,7 +93,7 @@ public class FormattingConversionServiceTests {
|
|||
return source.toDate();
|
||||
}
|
||||
});
|
||||
formattingService.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory());
|
||||
formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
|
||||
String formatted = (String) formattingService.convert(new LocalDate(2009, 10, 31).toDateTimeAtCurrentTime()
|
||||
.toDate(), new TypeDescriptor(Model.class.getField("date")), TypeDescriptor.valueOf(String.class));
|
||||
assertEquals("10/31/09", formatted);
|
||||
|
|
|
|||
Loading…
Reference in New Issue