Browse Source

Avoid defensive checks against java.time API

Issue: SPR-13188
pull/1208/merge
Juergen Hoeller 9 years ago
parent
commit
85b0ce1ef7
  1. 18
      spring-beans/src/main/java/org/springframework/beans/PropertyEditorRegistrySupport.java
  2. 15
      spring-context/src/main/java/org/springframework/format/support/DefaultFormattingConversionService.java
  3. 25
      spring-core/src/main/java/org/springframework/core/convert/support/DefaultConversionService.java
  4. 17
      spring-web/src/main/java/org/springframework/http/converter/json/Jackson2ObjectMapperBuilder.java
  5. 19
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ServletRequestMethodArgumentResolver.java

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

@ -26,6 +26,7 @@ import java.net.URI; @@ -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; @@ -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 { @@ -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.

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

@ -1,5 +1,5 @@ @@ -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; @@ -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 @@ -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 @@ -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);

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

@ -23,7 +23,6 @@ import java.util.UUID; @@ -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; @@ -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 { @@ -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 { @@ -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());
}
}
}

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

@ -753,16 +753,13 @@ public class Jackson2ObjectMapperBuilder { @@ -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<? extends Module> javaTimeModule = (Class<? extends Module>)
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<? extends Module> javaTimeModule = (Class<? extends Module>)
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?

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

@ -70,7 +70,7 @@ public class ServletRequestMethodArgumentResolver implements HandlerMethodArgume @@ -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 @@ -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 @@ -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());
}
}
}

Loading…
Cancel
Save