Skip to content

Commit 85b0ce1

Browse files
committed
Avoid defensive checks against java.time API
Issue: SPR-13188
1 parent a0fee46 commit 85b0ce1

File tree

5 files changed

+22
-72
lines changed

5 files changed

+22
-72
lines changed

spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.net.URL;
2727
import java.nio.charset.Charset;
2828
import java.nio.file.Path;
29+
import java.time.ZoneId;
2930
import java.util.Collection;
3031
import java.util.Currency;
3132
import java.util.HashMap;
@@ -89,19 +90,6 @@
8990
*/
9091
public class PropertyEditorRegistrySupport implements PropertyEditorRegistry {
9192

92-
private static Class<?> zoneIdClass;
93-
94-
static {
95-
try {
96-
zoneIdClass = ClassUtils.forName("java.time.ZoneId", PropertyEditorRegistrySupport.class.getClassLoader());
97-
}
98-
catch (ClassNotFoundException ex) {
99-
// Java 8 ZoneId class not available
100-
zoneIdClass = null;
101-
}
102-
}
103-
104-
10593
private ConversionService conversionService;
10694

10795
private boolean defaultEditorsActive = false;
@@ -222,9 +210,7 @@ private void createDefaultEditors() {
222210
this.defaultEditors.put(URI.class, new URIEditor());
223211
this.defaultEditors.put(URL.class, new URLEditor());
224212
this.defaultEditors.put(UUID.class, new UUIDEditor());
225-
if (zoneIdClass != null) {
226-
this.defaultEditors.put(zoneIdClass, new ZoneIdEditor());
227-
}
213+
this.defaultEditors.put(ZoneId.class, new ZoneIdEditor());
228214

229215
// Default instances of collection editors.
230216
// Can be overridden by registering custom instances of those as custom editors.

spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,10 +21,10 @@
2121
import org.springframework.format.datetime.DateFormatterRegistrar;
2222
import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar;
2323
import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
24+
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
2425
import org.springframework.format.number.money.CurrencyUnitFormatter;
2526
import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory;
2627
import org.springframework.format.number.money.MonetaryAmountFormatter;
27-
import org.springframework.format.number.NumberFormatAnnotationFormatterFactory;
2828
import org.springframework.util.ClassUtils;
2929
import org.springframework.util.StringValueResolver;
3030

@@ -49,9 +49,6 @@ public class DefaultFormattingConversionService extends FormattingConversionServ
4949
private static final boolean jsr354Present = ClassUtils.isPresent(
5050
"javax.money.MonetaryAmount", DefaultFormattingConversionService.class.getClassLoader());
5151

52-
private static final boolean jsr310Present = ClassUtils.isPresent(
53-
"java.time.LocalDate", DefaultFormattingConversionService.class.getClassLoader());
54-
5552
private static final boolean jodaTimePresent = ClassUtils.isPresent(
5653
"org.joda.time.LocalDate", DefaultFormattingConversionService.class.getClassLoader());
5754

@@ -112,10 +109,10 @@ public static void addDefaultFormatters(FormatterRegistry formatterRegistry) {
112109
}
113110

114111
// Default handling of date-time values
115-
if (jsr310Present) {
116-
// just handling JSR-310 specific date and time types
117-
new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry);
118-
}
112+
113+
// just handling JSR-310 specific date and time types
114+
new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry);
115+
119116
if (jodaTimePresent) {
120117
// handles Joda-specific types as well as Date, Calendar, Long
121118
new JodaTimeFormatterRegistrar().registerFormatters(formatterRegistry);

spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.core.convert.ConversionService;
2525
import org.springframework.core.convert.converter.ConverterRegistry;
26-
import org.springframework.util.ClassUtils;
2726

2827
/**
2928
* A specialization of {@link GenericConversionService} configured by default with
@@ -40,11 +39,6 @@
4039
*/
4140
public class DefaultConversionService extends GenericConversionService {
4241

43-
/** Java 8's java.time package available? */
44-
private static final boolean jsr310Available =
45-
ClassUtils.isPresent("java.time.ZoneId", DefaultConversionService.class.getClassLoader());
46-
47-
4842
/**
4943
* Create a new {@code DefaultConversionService} with the set of
5044
* {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}.
@@ -67,9 +61,9 @@ public static void addDefaultConverters(ConverterRegistry converterRegistry) {
6761
addCollectionConverters(converterRegistry);
6862

6963
converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry));
70-
if (jsr310Available) {
71-
Jsr310ConverterRegistrar.registerJsr310Converters(converterRegistry);
72-
}
64+
converterRegistry.addConverter(new StringToTimeZoneConverter());
65+
converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());
66+
converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());
7367

7468
converterRegistry.addConverter(new ObjectToObjectConverter());
7569
converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry));
@@ -149,17 +143,4 @@ private static void addScalarConverters(ConverterRegistry converterRegistry) {
149143
converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter());
150144
}
151145

152-
153-
/**
154-
* Inner class to avoid a hard-coded dependency on Java 8's {@code java.time} package.
155-
*/
156-
private static final class Jsr310ConverterRegistrar {
157-
158-
public static void registerJsr310Converters(ConverterRegistry converterRegistry) {
159-
converterRegistry.addConverter(new StringToTimeZoneConverter());
160-
converterRegistry.addConverter(new ZoneIdToTimeZoneConverter());
161-
converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter());
162-
}
163-
}
164-
165146
}

spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -753,16 +753,13 @@ private void registerWellKnownModulesIfAvailable(ObjectMapper objectMapper) {
753753
// jackson-datatype-jdk8 not available
754754
}
755755

756-
// Java 8 java.time package present?
757-
if (ClassUtils.isPresent("java.time.LocalDate", this.moduleClassLoader)) {
758-
try {
759-
Class<? extends Module> javaTimeModule = (Class<? extends Module>)
760-
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader);
761-
objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule));
762-
}
763-
catch (ClassNotFoundException ex) {
764-
// jackson-datatype-jsr310 not available
765-
}
756+
try {
757+
Class<? extends Module> javaTimeModule = (Class<? extends Module>)
758+
ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader);
759+
objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule));
760+
}
761+
catch (ClassNotFoundException ex) {
762+
// jackson-datatype-jsr310 not available
766763
}
767764

768765
// Joda-Time present?

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public boolean supportsParameter(MethodParameter parameter) {
7070
Principal.class.isAssignableFrom(paramType) ||
7171
Locale.class == paramType ||
7272
TimeZone.class == paramType ||
73-
"java.time.ZoneId".equals(paramType.getName()) ||
73+
ZoneId.class == paramType ||
7474
InputStream.class.isAssignableFrom(paramType) ||
7575
Reader.class.isAssignableFrom(paramType) ||
7676
HttpMethod.class == paramType);
@@ -110,8 +110,9 @@ else if (TimeZone.class == paramType) {
110110
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
111111
return (timeZone != null ? timeZone : TimeZone.getDefault());
112112
}
113-
else if ("java.time.ZoneId".equals(paramType.getName())) {
114-
return ZoneIdResolver.resolveZoneId(request);
113+
else if (ZoneId.class == paramType) {
114+
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
115+
return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
115116
}
116117
else if (InputStream.class.isAssignableFrom(paramType)) {
117118
return request.getInputStream();
@@ -126,16 +127,4 @@ else if (Reader.class.isAssignableFrom(paramType)) {
126127
}
127128
}
128129

129-
130-
/**
131-
* Inner class to avoid a hard-coded dependency on Java 8's {@link java.time.ZoneId}.
132-
*/
133-
private static class ZoneIdResolver {
134-
135-
public static Object resolveZoneId(HttpServletRequest request) {
136-
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
137-
return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
138-
}
139-
}
140-
141130
}

0 commit comments

Comments
 (0)