Skip to content

Commit ea41051

Browse files
committed
Do not invoke [Map|Collection].isEmpty() in nullSafeConciseToString()
gh-30810 introduced explicit support for collections and maps in ObjectUtils.nullSafeConciseToString() by invoking isEmpty() on a Map or Collection to determine which concise string representation should be used. However, this caused a regression in which an exception was thrown if the Map or Collection was a proxy generated by AbstractFactoryBean to support <util:set />, <util:list />, and <util:map /> in XML configuration. This commit addresses this set of regressions by always returning "[...]" or "{...}" for a Collection or Map, respectively, disregarding whether the map is empty or not. Closes gh-31138
1 parent 311c58e commit ea41051

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

spring-core/src/main/java/org/springframework/util/ObjectUtils.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public abstract class ObjectUtils {
7070
private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
7171
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
7272
private static final String NON_EMPTY_ARRAY = ARRAY_START + "..." + ARRAY_END;
73-
private static final String EMPTY_COLLECTION = "[]";
7473
private static final String NON_EMPTY_COLLECTION = "[...]";
7574

7675

@@ -915,10 +914,9 @@ public static String nullSafeToString(@Nullable short[] array) {
915914
* <li>{@code"Optional[<concise-string>]"} if {@code obj} is a non-empty {@code Optional},
916915
* where {@code <concise-string>} is the result of invoking {@link #nullSafeConciseToString}
917916
* on the object contained in the {@code Optional}</li>
918-
* <li>{@code "{}"} if {@code obj} is an empty array or {@link Map}</li>
919-
* <li>{@code "{...}"} if {@code obj} is a non-empty array or {@link Map}</li>
920-
* <li>{@code "[]"} if {@code obj} is an empty {@link Collection}</li>
921-
* <li>{@code "[...]"} if {@code obj} is a non-empty {@link Collection}</li>
917+
* <li>{@code "{}"} if {@code obj} is an empty array</li>
918+
* <li>{@code "{...}"} if {@code obj} is a {@link Map} or a non-empty array</li>
919+
* <li>{@code "[...]"} if {@code obj} is a {@link Collection}</li>
922920
* <li>{@linkplain Class#getName() Class name} if {@code obj} is a {@link Class}</li>
923921
* <li>{@linkplain Charset#name() Charset name} if {@code obj} is a {@link Charset}</li>
924922
* <li>{@linkplain TimeZone#getID() TimeZone ID} if {@code obj} is a {@link TimeZone}</li>
@@ -953,12 +951,12 @@ public static String nullSafeConciseToString(@Nullable Object obj) {
953951
if (obj.getClass().isArray()) {
954952
return (Array.getLength(obj) == 0 ? EMPTY_ARRAY : NON_EMPTY_ARRAY);
955953
}
956-
if (obj instanceof Collection<?> collection) {
957-
return (collection.isEmpty() ? EMPTY_COLLECTION : NON_EMPTY_COLLECTION);
954+
if (obj instanceof Collection) {
955+
return NON_EMPTY_COLLECTION;
958956
}
959-
if (obj instanceof Map<?, ?> map) {
960-
// EMPTY_ARRAY and NON_EMPTY_ARRAY are also used for maps.
961-
return (map.isEmpty() ? EMPTY_ARRAY : NON_EMPTY_ARRAY);
957+
if (obj instanceof Map) {
958+
// NON_EMPTY_ARRAY is also used for maps.
959+
return NON_EMPTY_ARRAY;
962960
}
963961
if (obj instanceof Class<?> clazz) {
964962
return clazz.getName();

spring-core/src/test/java/org/springframework/util/ObjectUtilsTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,8 @@ void nullSafeConciseToStringForNonEmptyArrays() {
10521052
void nullSafeConciseToStringForEmptyCollections() {
10531053
List<String> list = List.of();
10541054
Set<Integer> set = Set.of();
1055-
assertThat(ObjectUtils.nullSafeConciseToString(list)).isEqualTo("[]");
1056-
assertThat(ObjectUtils.nullSafeConciseToString(set)).isEqualTo("[]");
1055+
assertThat(ObjectUtils.nullSafeConciseToString(list)).isEqualTo("[...]");
1056+
assertThat(ObjectUtils.nullSafeConciseToString(set)).isEqualTo("[...]");
10571057
}
10581058

10591059
@Test
@@ -1066,8 +1066,8 @@ void nullSafeConciseToStringForNonEmptyCollections() {
10661066

10671067
@Test
10681068
void nullSafeConciseToStringForEmptyMaps() {
1069-
Map<String, String> map = new HashMap<String, String>();
1070-
assertThat(ObjectUtils.nullSafeConciseToString(map)).isEqualTo("{}");
1069+
Map<String, String> map = Map.of();
1070+
assertThat(ObjectUtils.nullSafeConciseToString(map)).isEqualTo("{...}");
10711071
}
10721072

10731073
@Test

0 commit comments

Comments
 (0)