From ab5aea5a1346cf489e8c727c3d4ee563352b3787 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 19 Jun 2014 21:25:46 +0200 Subject: [PATCH] Defensively check javaUtilOptionalEmpty Issue: SPR-11888 --- .../convert/support/GenericConversionService.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java index a26e2805d1..525b9f250d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java +++ b/spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java @@ -59,9 +59,6 @@ import org.springframework.util.StringUtils; */ public class GenericConversionService implements ConfigurableConversionService { - /** Java 8's java.util.Optional.empty() */ - private static Object javaUtilOptionalEmpty = null; - /** * General NO-OP converter used when conversion is not required. */ @@ -73,11 +70,16 @@ public class GenericConversionService implements ConfigurableConversionService { */ private static final GenericConverter NO_MATCH = new NoOpConverter("NO_MATCH"); + + /** Java 8's java.util.Optional.empty() */ + private static Object javaUtilOptionalEmpty = null; + static { try { Class clazz = ClassUtils.forName("java.util.Optional", GenericConversionService.class.getClassLoader()); javaUtilOptionalEmpty = ClassUtils.getMethod(clazz, "empty").invoke(null); - } catch (Exception ex) { + } + catch (Exception ex) { // Java 8 not available - conversion to Optional not supported then. } } @@ -225,7 +227,7 @@ public class GenericConversionService implements ConfigurableConversionService { * @return the converted null object */ protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) { - if (targetType.getObjectType().equals(javaUtilOptionalEmpty.getClass())) { + if (javaUtilOptionalEmpty != null && targetType.getObjectType().equals(javaUtilOptionalEmpty.getClass())) { return javaUtilOptionalEmpty; } return null;