Skip to content

Commit a3ebf13

Browse files
committed
SelectedValueComparator defensively handles null values in exhaustiveCompare
Issue: SPR-12001 (cherry picked from commit 980f971)
1 parent ab9d947 commit a3ebf13

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/SelectedValueComparator.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -94,18 +94,18 @@ else if (boundValue == null) {
9494
selected = collectionCompare(CollectionUtils.arrayToList(boundValue), candidateValue, bindStatus);
9595
}
9696
else if (boundValue instanceof Collection) {
97-
selected = collectionCompare((Collection) boundValue, candidateValue, bindStatus);
97+
selected = collectionCompare((Collection<?>) boundValue, candidateValue, bindStatus);
9898
}
9999
else if (boundValue instanceof Map) {
100-
selected = mapCompare((Map) boundValue, candidateValue, bindStatus);
100+
selected = mapCompare((Map<?, ?>) boundValue, candidateValue, bindStatus);
101101
}
102102
if (!selected) {
103103
selected = exhaustiveCompare(boundValue, candidateValue, bindStatus.getEditor(), null);
104104
}
105105
return selected;
106106
}
107107

108-
private static boolean collectionCompare(Collection boundCollection, Object candidateValue, BindStatus bindStatus) {
108+
private static boolean collectionCompare(Collection<?> boundCollection, Object candidateValue, BindStatus bindStatus) {
109109
try {
110110
if (boundCollection.contains(candidateValue)) {
111111
return true;
@@ -117,7 +117,7 @@ private static boolean collectionCompare(Collection boundCollection, Object cand
117117
return exhaustiveCollectionCompare(boundCollection, candidateValue, bindStatus);
118118
}
119119

120-
private static boolean mapCompare(Map boundMap, Object candidateValue, BindStatus bindStatus) {
120+
private static boolean mapCompare(Map<?, ?> boundMap, Object candidateValue, BindStatus bindStatus) {
121121
try {
122122
if (boundMap.containsKey(candidateValue)) {
123123
return true;
@@ -130,7 +130,7 @@ private static boolean mapCompare(Map boundMap, Object candidateValue, BindStatu
130130
}
131131

132132
private static boolean exhaustiveCollectionCompare(
133-
Collection collection, Object candidateValue, BindStatus bindStatus) {
133+
Collection<?> collection, Object candidateValue, BindStatus bindStatus) {
134134

135135
Map<PropertyEditor, Object> convertedValueCache = new HashMap<PropertyEditor, Object>(1);
136136
PropertyEditor editor = null;
@@ -164,8 +164,8 @@ private static boolean exhaustiveCompare(Object boundValue, Object candidate,
164164
return true;
165165
}
166166
}
167-
else if (boundValue.getClass().isEnum()) {
168-
Enum boundEnum = (Enum) boundValue;
167+
else if (boundValue != null && boundValue.getClass().isEnum()) {
168+
Enum<?> boundEnum = (Enum<?>) boundValue;
169169
String enumCodeAsString = ObjectUtils.getDisplayString(boundEnum.name());
170170
if (enumCodeAsString.equals(candidateDisplayString)) {
171171
return true;

0 commit comments

Comments
 (0)