JodaTimeFormatterRegistrar defensively checks whether Joda-Time 2.x is present (for Joda-Time 1.x tolerance on the classpath)

Also switches 4.2.4's new formatter implementations to package visibility, just in case they'll be superseded by another variant in the future.

Issue: SPR-13730
This commit is contained in:
Juergen Hoeller 2015-11-29 22:22:05 +01:00
parent 3234d9ede3
commit edbb8bbb01
9 changed files with 32 additions and 10 deletions

View File

@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
* @since 4.2.4 * @since 4.2.4
* @see Duration#parse * @see Duration#parse
*/ */
public class DurationFormatter implements Formatter<Duration> { class DurationFormatter implements Formatter<Duration> {
@Override @Override
public Duration parse(String text, Locale locale) throws ParseException { public Duration parse(String text, Locale locale) throws ParseException {

View File

@ -38,6 +38,7 @@ import org.springframework.format.FormatterRegistry;
import org.springframework.format.Parser; import org.springframework.format.Parser;
import org.springframework.format.Printer; import org.springframework.format.Printer;
import org.springframework.format.annotation.DateTimeFormat.ISO; import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.util.ClassUtils;
/** /**
* Configures Joda-Time's formatting system for use with Spring. * Configures Joda-Time's formatting system for use with Spring.
@ -61,6 +62,14 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
private enum Type {DATE, TIME, DATE_TIME} private enum Type {DATE, TIME, DATE_TIME}
/**
* Strictly speaking, this should not be necessary since we formally require JodaTime 2.x.
* However, since Joda-Time formatters are being registered automatically, we defensively
* adapt to Joda-Time 1.x when encountered on the classpath. To be removed in Spring 5.0.
*/
private static final boolean jodaTime2Available = ClassUtils.isPresent(
"org.joda.time.YearMonth", JodaTimeFormatterRegistrar.class.getClassLoader());
/** /**
* User defined formatters. * User defined formatters.
*/ */
@ -200,8 +209,9 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
registry.addFormatterForFieldType(Period.class, new PeriodFormatter()); registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
registry.addFormatterForFieldType(Duration.class, new DurationFormatter()); registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter()); if (jodaTime2Available) {
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter()); JodaTime2Delegate.registerAdditionalFormatters(registry);
}
registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
} }
@ -231,4 +241,16 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
} }
} }
/**
* Inner class to avoid a hard dependency on Joda-Time 2.x.
*/
private static class JodaTime2Delegate {
public static void registerAdditionalFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
}
}
} }

View File

@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
* @since 4.2.4 * @since 4.2.4
* @see MonthDay#parse * @see MonthDay#parse
*/ */
public class MonthDayFormatter implements Formatter<MonthDay> { class MonthDayFormatter implements Formatter<MonthDay> {
@Override @Override
public MonthDay parse(String text, Locale locale) throws ParseException { public MonthDay parse(String text, Locale locale) throws ParseException {

View File

@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
* @since 4.2.4 * @since 4.2.4
* @see Period#parse * @see Period#parse
*/ */
public class PeriodFormatter implements Formatter<Period> { class PeriodFormatter implements Formatter<Period> {
@Override @Override
public Period parse(String text, Locale locale) throws ParseException { public Period parse(String text, Locale locale) throws ParseException {

View File

@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
* @since 4.2.4 * @since 4.2.4
* @see YearMonth#parse * @see YearMonth#parse
*/ */
public class YearMonthFormatter implements Formatter<YearMonth> { class YearMonthFormatter implements Formatter<YearMonth> {
@Override @Override
public YearMonth parse(String text, Locale locale) throws ParseException { public YearMonth parse(String text, Locale locale) throws ParseException {

View File

@ -32,7 +32,7 @@ import org.springframework.lang.UsesJava8;
* @see Duration#parse * @see Duration#parse
*/ */
@UsesJava8 @UsesJava8
public class DurationFormatter implements Formatter<Duration> { class DurationFormatter implements Formatter<Duration> {
@Override @Override
public Duration parse(String text, Locale locale) throws ParseException { public Duration parse(String text, Locale locale) throws ParseException {

View File

@ -32,7 +32,7 @@ import org.springframework.lang.UsesJava8;
* @see MonthDay#parse * @see MonthDay#parse
*/ */
@UsesJava8 @UsesJava8
public class MonthDayFormatter implements Formatter<MonthDay> { class MonthDayFormatter implements Formatter<MonthDay> {
@Override @Override
public MonthDay parse(String text, Locale locale) throws ParseException { public MonthDay parse(String text, Locale locale) throws ParseException {

View File

@ -33,7 +33,7 @@ import org.springframework.lang.UsesJava8;
* @see Period#parse * @see Period#parse
*/ */
@UsesJava8 @UsesJava8
public class PeriodFormatter implements Formatter<Period> { class PeriodFormatter implements Formatter<Period> {
@Override @Override
public Period parse(String text, Locale locale) throws ParseException { public Period parse(String text, Locale locale) throws ParseException {

View File

@ -32,7 +32,7 @@ import org.springframework.lang.UsesJava8;
* @see YearMonth#parse * @see YearMonth#parse
*/ */
@UsesJava8 @UsesJava8
public class YearMonthFormatter implements Formatter<YearMonth> { class YearMonthFormatter implements Formatter<YearMonth> {
@Override @Override
public YearMonth parse(String text, Locale locale) throws ParseException { public YearMonth parse(String text, Locale locale) throws ParseException {