Skip to content

Commit ab5aea5

Browse files
committed
Defensively check javaUtilOptionalEmpty
Issue: SPR-11888
1 parent bc1f19c commit ab5aea5

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@
5959
*/
6060
public class GenericConversionService implements ConfigurableConversionService {
6161

62-
/** Java 8's java.util.Optional.empty() */
63-
private static Object javaUtilOptionalEmpty = null;
64-
6562
/**
6663
* General NO-OP converter used when conversion is not required.
6764
*/
@@ -73,11 +70,16 @@ public class GenericConversionService implements ConfigurableConversionService {
7370
*/
7471
private static final GenericConverter NO_MATCH = new NoOpConverter("NO_MATCH");
7572

73+
74+
/** Java 8's java.util.Optional.empty() */
75+
private static Object javaUtilOptionalEmpty = null;
76+
7677
static {
7778
try {
7879
Class<?> clazz = ClassUtils.forName("java.util.Optional", GenericConversionService.class.getClassLoader());
7980
javaUtilOptionalEmpty = ClassUtils.getMethod(clazz, "empty").invoke(null);
80-
} catch (Exception ex) {
81+
}
82+
catch (Exception ex) {
8183
// Java 8 not available - conversion to Optional not supported then.
8284
}
8385
}
@@ -225,7 +227,7 @@ public String toString() {
225227
* @return the converted null object
226228
*/
227229
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
228-
if (targetType.getObjectType().equals(javaUtilOptionalEmpty.getClass())) {
230+
if (javaUtilOptionalEmpty != null && targetType.getObjectType().equals(javaUtilOptionalEmpty.getClass())) {
229231
return javaUtilOptionalEmpty;
230232
}
231233
return null;

0 commit comments

Comments
 (0)