Skip to content

Commit edbb8bb

Browse files
committed
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
1 parent 3234d9e commit edbb8bb

File tree

9 files changed

+32
-10
lines changed

9 files changed

+32
-10
lines changed

spring-context/src/main/java/org/springframework/format/datetime/joda/DurationFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @since 4.2.4
3232
* @see Duration#parse
3333
*/
34-
public class DurationFormatter implements Formatter<Duration> {
34+
class DurationFormatter implements Formatter<Duration> {
3535

3636
@Override
3737
public Duration parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/joda/JodaTimeFormatterRegistrar.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.format.Parser;
3939
import org.springframework.format.Printer;
4040
import org.springframework.format.annotation.DateTimeFormat.ISO;
41+
import org.springframework.util.ClassUtils;
4142

4243
/**
4344
* Configures Joda-Time's formatting system for use with Spring.
@@ -61,6 +62,14 @@ public class JodaTimeFormatterRegistrar implements FormatterRegistrar {
6162
private enum Type {DATE, TIME, DATE_TIME}
6263

6364

65+
/**
66+
* Strictly speaking, this should not be necessary since we formally require JodaTime 2.x.
67+
* However, since Joda-Time formatters are being registered automatically, we defensively
68+
* adapt to Joda-Time 1.x when encountered on the classpath. To be removed in Spring 5.0.
69+
*/
70+
private static final boolean jodaTime2Available = ClassUtils.isPresent(
71+
"org.joda.time.YearMonth", JodaTimeFormatterRegistrar.class.getClassLoader());
72+
6473
/**
6574
* User defined formatters.
6675
*/
@@ -200,8 +209,9 @@ public void registerFormatters(FormatterRegistry registry) {
200209

201210
registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
202211
registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
203-
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
204-
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
212+
if (jodaTime2Available) {
213+
JodaTime2Delegate.registerAdditionalFormatters(registry);
214+
}
205215

206216
registry.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory());
207217
}
@@ -231,4 +241,16 @@ private void addFormatterForFields(FormatterRegistry registry, Printer<?> printe
231241
}
232242
}
233243

244+
245+
/**
246+
* Inner class to avoid a hard dependency on Joda-Time 2.x.
247+
*/
248+
private static class JodaTime2Delegate {
249+
250+
public static void registerAdditionalFormatters(FormatterRegistry registry) {
251+
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
252+
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
253+
}
254+
}
255+
234256
}

spring-context/src/main/java/org/springframework/format/datetime/joda/MonthDayFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @since 4.2.4
3232
* @see MonthDay#parse
3333
*/
34-
public class MonthDayFormatter implements Formatter<MonthDay> {
34+
class MonthDayFormatter implements Formatter<MonthDay> {
3535

3636
@Override
3737
public MonthDay parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/joda/PeriodFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @since 4.2.4
3232
* @see Period#parse
3333
*/
34-
public class PeriodFormatter implements Formatter<Period> {
34+
class PeriodFormatter implements Formatter<Period> {
3535

3636
@Override
3737
public Period parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/joda/YearMonthFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* @since 4.2.4
3232
* @see YearMonth#parse
3333
*/
34-
public class YearMonthFormatter implements Formatter<YearMonth> {
34+
class YearMonthFormatter implements Formatter<YearMonth> {
3535

3636
@Override
3737
public YearMonth parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @see Duration#parse
3333
*/
3434
@UsesJava8
35-
public class DurationFormatter implements Formatter<Duration> {
35+
class DurationFormatter implements Formatter<Duration> {
3636

3737
@Override
3838
public Duration parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/standard/MonthDayFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @see MonthDay#parse
3333
*/
3434
@UsesJava8
35-
public class MonthDayFormatter implements Formatter<MonthDay> {
35+
class MonthDayFormatter implements Formatter<MonthDay> {
3636

3737
@Override
3838
public MonthDay parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/standard/PeriodFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* @see Period#parse
3434
*/
3535
@UsesJava8
36-
public class PeriodFormatter implements Formatter<Period> {
36+
class PeriodFormatter implements Formatter<Period> {
3737

3838
@Override
3939
public Period parse(String text, Locale locale) throws ParseException {

spring-context/src/main/java/org/springframework/format/datetime/standard/YearMonthFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @see YearMonth#parse
3333
*/
3434
@UsesJava8
35-
public class YearMonthFormatter implements Formatter<YearMonth> {
35+
class YearMonthFormatter implements Formatter<YearMonth> {
3636

3737
@Override
3838
public YearMonth parse(String text, Locale locale) throws ParseException {

0 commit comments

Comments
 (0)