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:
parent
3234d9ede3
commit
edbb8bbb01
|
|
@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
|
|||
* @since 4.2.4
|
||||
* @see Duration#parse
|
||||
*/
|
||||
public class DurationFormatter implements Formatter<Duration> {
|
||||
class DurationFormatter implements Formatter<Duration> {
|
||||
|
||||
@Override
|
||||
public Duration parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ import org.springframework.format.FormatterRegistry;
|
|||
import org.springframework.format.Parser;
|
||||
import org.springframework.format.Printer;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* 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}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
|
@ -200,8 +209,9 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
|
|||
|
||||
registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
|
||||
registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
|
||||
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
|
||||
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
|
||||
if (jodaTime2Available) {
|
||||
JodaTime2Delegate.registerAdditionalFormatters(registry);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
|
|||
* @since 4.2.4
|
||||
* @see MonthDay#parse
|
||||
*/
|
||||
public class MonthDayFormatter implements Formatter<MonthDay> {
|
||||
class MonthDayFormatter implements Formatter<MonthDay> {
|
||||
|
||||
@Override
|
||||
public MonthDay parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
|
|||
* @since 4.2.4
|
||||
* @see Period#parse
|
||||
*/
|
||||
public class PeriodFormatter implements Formatter<Period> {
|
||||
class PeriodFormatter implements Formatter<Period> {
|
||||
|
||||
@Override
|
||||
public Period parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ import org.springframework.format.Formatter;
|
|||
* @since 4.2.4
|
||||
* @see YearMonth#parse
|
||||
*/
|
||||
public class YearMonthFormatter implements Formatter<YearMonth> {
|
||||
class YearMonthFormatter implements Formatter<YearMonth> {
|
||||
|
||||
@Override
|
||||
public YearMonth parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import org.springframework.lang.UsesJava8;
|
|||
* @see Duration#parse
|
||||
*/
|
||||
@UsesJava8
|
||||
public class DurationFormatter implements Formatter<Duration> {
|
||||
class DurationFormatter implements Formatter<Duration> {
|
||||
|
||||
@Override
|
||||
public Duration parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import org.springframework.lang.UsesJava8;
|
|||
* @see MonthDay#parse
|
||||
*/
|
||||
@UsesJava8
|
||||
public class MonthDayFormatter implements Formatter<MonthDay> {
|
||||
class MonthDayFormatter implements Formatter<MonthDay> {
|
||||
|
||||
@Override
|
||||
public MonthDay parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ import org.springframework.lang.UsesJava8;
|
|||
* @see Period#parse
|
||||
*/
|
||||
@UsesJava8
|
||||
public class PeriodFormatter implements Formatter<Period> {
|
||||
class PeriodFormatter implements Formatter<Period> {
|
||||
|
||||
@Override
|
||||
public Period parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ import org.springframework.lang.UsesJava8;
|
|||
* @see YearMonth#parse
|
||||
*/
|
||||
@UsesJava8
|
||||
public class YearMonthFormatter implements Formatter<YearMonth> {
|
||||
class YearMonthFormatter implements Formatter<YearMonth> {
|
||||
|
||||
@Override
|
||||
public YearMonth parse(String text, Locale locale) throws ParseException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue