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
This commit is contained in:
parent
311c58ea2d
commit
ea41051651
|
|
@ -70,7 +70,6 @@ public abstract class ObjectUtils {
|
|||
private static final String ARRAY_ELEMENT_SEPARATOR = ", ";
|
||||
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
|
||||
private static final String NON_EMPTY_ARRAY = ARRAY_START + "..." + ARRAY_END;
|
||||
private static final String EMPTY_COLLECTION = "[]";
|
||||
private static final String NON_EMPTY_COLLECTION = "[...]";
|
||||
|
||||
|
||||
|
|
@ -915,10 +914,9 @@ public abstract class ObjectUtils {
|
|||
* <li>{@code"Optional[<concise-string>]"} if {@code obj} is a non-empty {@code Optional},
|
||||
* where {@code <concise-string>} is the result of invoking {@link #nullSafeConciseToString}
|
||||
* on the object contained in the {@code Optional}</li>
|
||||
* <li>{@code "{}"} if {@code obj} is an empty array or {@link Map}</li>
|
||||
* <li>{@code "{...}"} if {@code obj} is a non-empty array or {@link Map}</li>
|
||||
* <li>{@code "[]"} if {@code obj} is an empty {@link Collection}</li>
|
||||
* <li>{@code "[...]"} if {@code obj} is a non-empty {@link Collection}</li>
|
||||
* <li>{@code "{}"} if {@code obj} is an empty array</li>
|
||||
* <li>{@code "{...}"} if {@code obj} is a {@link Map} or a non-empty array</li>
|
||||
* <li>{@code "[...]"} if {@code obj} is a {@link Collection}</li>
|
||||
* <li>{@linkplain Class#getName() Class name} if {@code obj} is a {@link Class}</li>
|
||||
* <li>{@linkplain Charset#name() Charset name} if {@code obj} is a {@link Charset}</li>
|
||||
* <li>{@linkplain TimeZone#getID() TimeZone ID} if {@code obj} is a {@link TimeZone}</li>
|
||||
|
|
@ -953,12 +951,12 @@ public abstract class ObjectUtils {
|
|||
if (obj.getClass().isArray()) {
|
||||
return (Array.getLength(obj) == 0 ? EMPTY_ARRAY : NON_EMPTY_ARRAY);
|
||||
}
|
||||
if (obj instanceof Collection<?> collection) {
|
||||
return (collection.isEmpty() ? EMPTY_COLLECTION : NON_EMPTY_COLLECTION);
|
||||
if (obj instanceof Collection) {
|
||||
return NON_EMPTY_COLLECTION;
|
||||
}
|
||||
if (obj instanceof Map<?, ?> map) {
|
||||
// EMPTY_ARRAY and NON_EMPTY_ARRAY are also used for maps.
|
||||
return (map.isEmpty() ? EMPTY_ARRAY : NON_EMPTY_ARRAY);
|
||||
if (obj instanceof Map) {
|
||||
// NON_EMPTY_ARRAY is also used for maps.
|
||||
return NON_EMPTY_ARRAY;
|
||||
}
|
||||
if (obj instanceof Class<?> clazz) {
|
||||
return clazz.getName();
|
||||
|
|
|
|||
|
|
@ -1052,8 +1052,8 @@ class ObjectUtilsTests {
|
|||
void nullSafeConciseToStringForEmptyCollections() {
|
||||
List<String> list = List.of();
|
||||
Set<Integer> set = Set.of();
|
||||
assertThat(ObjectUtils.nullSafeConciseToString(list)).isEqualTo("[]");
|
||||
assertThat(ObjectUtils.nullSafeConciseToString(set)).isEqualTo("[]");
|
||||
assertThat(ObjectUtils.nullSafeConciseToString(list)).isEqualTo("[...]");
|
||||
assertThat(ObjectUtils.nullSafeConciseToString(set)).isEqualTo("[...]");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -1066,8 +1066,8 @@ class ObjectUtilsTests {
|
|||
|
||||
@Test
|
||||
void nullSafeConciseToStringForEmptyMaps() {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
assertThat(ObjectUtils.nullSafeConciseToString(map)).isEqualTo("{}");
|
||||
Map<String, String> map = Map.of();
|
||||
assertThat(ObjectUtils.nullSafeConciseToString(map)).isEqualTo("{...}");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue