From 85b0ce1ef78ea0aa795e80821e206cc851c9894b Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 22 Nov 2016 14:55:03 +0100 Subject: [PATCH] Avoid defensive checks against java.time API Issue: SPR-13188 --- .../beans/PropertyEditorRegistrySupport.java | 18 ++----------- .../DefaultFormattingConversionService.java | 15 +++++------ .../support/DefaultConversionService.java | 25 +++---------------- .../json/Jackson2ObjectMapperBuilder.java | 17 ++++++------- .../ServletRequestMethodArgumentResolver.java | 19 +++----------- 5 files changed, 22 insertions(+), 72 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java b/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java index cad28903a6..c28a2cf269 100644 --- a/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java +++ b/spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java @@ -26,6 +26,7 @@ import java.net.URI; import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Path; +import java.time.ZoneId; import java.util.Collection; import java.util.Currency; import java.util.HashMap; @@ -89,19 +90,6 @@ import org.springframework.util.ClassUtils; */ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry { - private static Class zoneIdClass; - - static { - try { - zoneIdClass = ClassUtils.forName("java.time.ZoneId", PropertyEditorRegistrySupport.class.getClassLoader()); - } - catch (ClassNotFoundException ex) { - // Java 8 ZoneId class not available - zoneIdClass = null; - } - } - - private ConversionService conversionService; private boolean defaultEditorsActive = false; @@ -222,9 +210,7 @@ public class PropertyEditorRegistrySupport implements PropertyEditorRegistry { this.defaultEditors.put(URI.class, new URIEditor()); this.defaultEditors.put(URL.class, new URLEditor()); this.defaultEditors.put(UUID.class, new UUIDEditor()); - if (zoneIdClass != null) { - this.defaultEditors.put(zoneIdClass, new ZoneIdEditor()); - } + this.defaultEditors.put(ZoneId.class, new ZoneIdEditor()); // Default instances of collection editors. // Can be overridden by registering custom instances of those as custom editors. diff --git a/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java b/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java index 2c7c60a041..ed7194c7cb 100644 --- a/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java +++ b/spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,10 +21,10 @@ import org.springframework.format.FormatterRegistry; import org.springframework.format.datetime.DateFormatterRegistrar; import org.springframework.format.datetime.joda.JodaTimeFormatterRegistrar; import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar; +import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; import org.springframework.format.number.money.CurrencyUnitFormatter; import org.springframework.format.number.money.Jsr354NumberFormatAnnotationFormatterFactory; import org.springframework.format.number.money.MonetaryAmountFormatter; -import org.springframework.format.number.NumberFormatAnnotationFormatterFactory; import org.springframework.util.ClassUtils; import org.springframework.util.StringValueResolver; @@ -49,9 +49,6 @@ public class DefaultFormattingConversionService extends FormattingConversionServ private static final boolean jsr354Present = ClassUtils.isPresent( "javax.money.MonetaryAmount", DefaultFormattingConversionService.class.getClassLoader()); - private static final boolean jsr310Present = ClassUtils.isPresent( - "java.time.LocalDate", DefaultFormattingConversionService.class.getClassLoader()); - private static final boolean jodaTimePresent = ClassUtils.isPresent( "org.joda.time.LocalDate", DefaultFormattingConversionService.class.getClassLoader()); @@ -112,10 +109,10 @@ public class DefaultFormattingConversionService extends FormattingConversionServ } // Default handling of date-time values - if (jsr310Present) { - // just handling JSR-310 specific date and time types - new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry); - } + + // just handling JSR-310 specific date and time types + new DateTimeFormatterRegistrar().registerFormatters(formatterRegistry); + if (jodaTimePresent) { // handles Joda-specific types as well as Date, Calendar, Long new JodaTimeFormatterRegistrar().registerFormatters(formatterRegistry); diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java index 2cc1c5b3fa..1530b8daad 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java @@ -23,7 +23,6 @@ import java.util.UUID; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.converter.ConverterRegistry; -import org.springframework.util.ClassUtils; /** * A specialization of {@link GenericConversionService} configured by default with @@ -40,11 +39,6 @@ import org.springframework.util.ClassUtils; */ public class DefaultConversionService extends GenericConversionService { - /** Java 8's java.time package available? */ - private static final boolean jsr310Available = - ClassUtils.isPresent("java.time.ZoneId", DefaultConversionService.class.getClassLoader()); - - /** * Create a new {@code DefaultConversionService} with the set of * {@linkplain DefaultConversionService#addDefaultConverters(ConverterRegistry) default converters}. @@ -67,9 +61,9 @@ public class DefaultConversionService extends GenericConversionService { addCollectionConverters(converterRegistry); converterRegistry.addConverter(new ByteBufferConverter((ConversionService) converterRegistry)); - if (jsr310Available) { - Jsr310ConverterRegistrar.registerJsr310Converters(converterRegistry); - } + converterRegistry.addConverter(new StringToTimeZoneConverter()); + converterRegistry.addConverter(new ZoneIdToTimeZoneConverter()); + converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter()); converterRegistry.addConverter(new ObjectToObjectConverter()); converterRegistry.addConverter(new IdToEntityConverter((ConversionService) converterRegistry)); @@ -149,17 +143,4 @@ public class DefaultConversionService extends GenericConversionService { converterRegistry.addConverter(UUID.class, String.class, new ObjectToStringConverter()); } - - /** - * Inner class to avoid a hard-coded dependency on Java 8's {@code java.time} package. - */ - private static final class Jsr310ConverterRegistrar { - - public static void registerJsr310Converters(ConverterRegistry converterRegistry) { - converterRegistry.addConverter(new StringToTimeZoneConverter()); - converterRegistry.addConverter(new ZoneIdToTimeZoneConverter()); - converterRegistry.addConverter(new ZonedDateTimeToCalendarConverter()); - } - } - } diff --git a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java index e840fa2ff6..b115d854d7 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java +++ b/spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java @@ -753,16 +753,13 @@ public class Jackson2ObjectMapperBuilder { // jackson-datatype-jdk8 not available } - // Java 8 java.time package present? - if (ClassUtils.isPresent("java.time.LocalDate", this.moduleClassLoader)) { - try { - Class javaTimeModule = (Class) - ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader); - objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule)); - } - catch (ClassNotFoundException ex) { - // jackson-datatype-jsr310 not available - } + try { + Class javaTimeModule = (Class) + ClassUtils.forName("com.fasterxml.jackson.datatype.jsr310.JavaTimeModule", this.moduleClassLoader); + objectMapper.registerModule(BeanUtils.instantiateClass(javaTimeModule)); + } + catch (ClassNotFoundException ex) { + // jackson-datatype-jsr310 not available } // Joda-Time present? diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java index a87660869b..b2c561a272 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java @@ -70,7 +70,7 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume Principal.class.isAssignableFrom(paramType) || Locale.class == paramType || TimeZone.class == paramType || - "java.time.ZoneId".equals(paramType.getName()) || + ZoneId.class == paramType || InputStream.class.isAssignableFrom(paramType) || Reader.class.isAssignableFrom(paramType) || HttpMethod.class == paramType); @@ -110,8 +110,9 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume TimeZone timeZone = RequestContextUtils.getTimeZone(request); return (timeZone != null ? timeZone : TimeZone.getDefault()); } - else if ("java.time.ZoneId".equals(paramType.getName())) { - return ZoneIdResolver.resolveZoneId(request); + else if (ZoneId.class == paramType) { + TimeZone timeZone = RequestContextUtils.getTimeZone(request); + return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()); } else if (InputStream.class.isAssignableFrom(paramType)) { return request.getInputStream(); @@ -126,16 +127,4 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume } } - - /** - * Inner class to avoid a hard-coded dependency on Java 8's {@link java.time.ZoneId}. - */ - private static class ZoneIdResolver { - - public static Object resolveZoneId(HttpServletRequest request) { - TimeZone timeZone = RequestContextUtils.getTimeZone(request); - return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()); - } - } - }