From 1d005e12afc014dd461a1cea19e118118b3a7045 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 7 Dec 2009 20:33:03 +0000 Subject: [PATCH] fail when @DateTimeFormat is being used without JodaTime on the classpath (SPR-6508) --- ...TimeFormatAnnotationFormatterFactory.java} | 28 +++++------ .../joda/JodaTimeFormattingConfigurer.java | 2 +- ...ormattingConversionServiceFactoryBean.java | 46 ++++++++++++++++++- .../FormattingConversionServiceTests.java | 4 +- 4 files changed, 60 insertions(+), 20 deletions(-) rename org.springframework.context/src/main/java/org/springframework/format/datetime/joda/{DateTimeFormatAnnotationFormatterFactory.java => JodaDateTimeFormatAnnotationFormatterFactory.java} (84%) diff --git a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java similarity index 84% rename from org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java rename to org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java index 8872b8d1ca0..51eeeddae2d 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/DateTimeFormatAnnotationFormatterFactory.java +++ b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaDateTimeFormatAnnotationFormatterFactory.java @@ -46,13 +46,22 @@ import org.springframework.util.StringUtils; * @since 3.0 * @see DateTimeFormat */ -public final class DateTimeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory { +public final class JodaDateTimeFormatAnnotationFormatterFactory implements AnnotationFormatterFactory { private final Set> fieldTypes; - public DateTimeFormatAnnotationFormatterFactory() { - this.fieldTypes = Collections.unmodifiableSet(createFieldTypes()); + public JodaDateTimeFormatAnnotationFormatterFactory() { + Set> rawFieldTypes = new HashSet>(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> getFieldTypes() { @@ -85,19 +94,6 @@ public final class DateTimeFormatAnnotationFormatterFactory implements Annotatio // internal helpers - private Set> createFieldTypes() { - Set> fieldTypes = new HashSet>(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()); diff --git a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormattingConfigurer.java b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormattingConfigurer.java index 313fa803564..45bb862f4e5 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormattingConfigurer.java +++ b/org.springframework.context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormattingConfigurer.java @@ -112,7 +112,7 @@ public class JodaTimeFormattingConfigurer { Printer readableInstantPrinter = new ReadableInstantPrinter(jodaDateTimeFormatter); formatterRegistry.addFormatterForFieldType(ReadableInstant.class, readableInstantPrinter, dateTimeParser); - formatterRegistry.addFormatterForFieldAnnotation(new DateTimeFormatAnnotationFormatterFactory()); + formatterRegistry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); } diff --git a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java index 80bb0455c80..dca9d1175c1 100644 --- a/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java +++ b/org.springframework.context/src/main/java/org/springframework/format/support/FormattingConversionServiceFactoryBean.java @@ -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; @@ -76,7 +86,41 @@ public class FormattingConversionServiceFactoryBean registry.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); 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 { + + private final Set> fieldTypes; + + public NoJodaDateTimeFormatAnnotationFormatterFactory() { + Set> rawFieldTypes = new HashSet>(4); + rawFieldTypes.add(Date.class); + rawFieldTypes.add(Calendar.class); + rawFieldTypes.add(Long.class); + this.fieldTypes = Collections.unmodifiableSet(rawFieldTypes); + } + + public Set> 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"); + } } } diff --git a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java index a8ac79b236e..4eaf0a07bbe 100644 --- a/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java +++ b/org.springframework.context/src/test/java/org/springframework/format/support/FormattingConversionServiceTests.java @@ -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);