Skip to content

Commit c2cc55e

Browse files
committed
Consider UUID as simple value type with concise toString output
Closes gh-30661 (cherry picked from commit 927d27b)
1 parent 9cff2ac commit c2cc55e

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828
import java.util.Optional;
2929
import java.util.StringJoiner;
30+
import java.util.UUID;
3031

3132
import org.springframework.lang.Nullable;
3233

@@ -926,14 +927,15 @@ public static String nullSafeToString(@Nullable short[] array) {
926927
* <li>Potentially {@linkplain StringUtils#truncate(CharSequence) truncated string}
927928
* if {@code obj} is a {@link String} or {@link CharSequence}</li>
928929
* <li>Potentially {@linkplain StringUtils#truncate(CharSequence) truncated string}
929-
* if {@code obj} is a <em>simple type</em> whose {@code toString()} method returns
930-
* a non-null value.</li>
930+
* if {@code obj} is a <em>simple value type</em> whose {@code toString()} method
931+
* returns a non-null value.</li>
931932
* <li>Otherwise, a string representation of the object's type name concatenated
932933
* with {@code @} and a hex string form of the object's identity hash code</li>
933934
* </ul>
934-
* <p>In the context of this method, a <em>simple type</em> is any of the following:
935-
* a primitive wrapper (excluding {@link Void}), an {@link Enum}, a {@link Number},
936-
* a {@link Date}, a {@link Temporal}, a {@link URI}, a {@link URL}, or a {@link Locale}.
935+
* <p>In the context of this method, a <em>simple value type</em> is any of the following:
936+
* a primitive wrapper (excluding {@code Void}), an {@code Enum}, a {@code Number},
937+
* a {@code Date}, a {@code Temporal}, a {@code UUID}, a {@code URI}, a {@code URL},
938+
* or a {@code Locale}.
937939
* @param obj the object to build a string representation for
938940
* @return a concise string representation of the supplied object
939941
* @since 5.3.27
@@ -961,13 +963,8 @@ public static String nullSafeConciseToString(@Nullable Object obj) {
961963
}
962964

963965
/**
964-
* Copy of {@link org.springframework.beans.BeanUtils#isSimpleValueType(Class)}.
965-
* <p>Check if the given type represents a "simple" value type: a primitive or
966-
* primitive wrapper, an enum, a String or other CharSequence, a Number, a
967-
* Date, a Temporal, a URI, a URL, a Locale, or a Class.
968-
* <p>{@code Void} and {@code void} are not considered simple value types.
969-
* @param type the type to check
970-
* @return whether the given type represents a "simple" value type
966+
* Derived from {@link org.springframework.beans.BeanUtils#isSimpleValueType}.
967+
* As of 5.3.28, considering {@code UUID} in addition to the bean-level check.
971968
*/
972969
private static boolean isSimpleValueType(Class<?> type) {
973970
return (Void.class != type && void.class != type &&
@@ -977,6 +974,7 @@ private static boolean isSimpleValueType(Class<?> type) {
977974
Number.class.isAssignableFrom(type) ||
978975
Date.class.isAssignableFrom(type) ||
979976
Temporal.class.isAssignableFrom(type) ||
977+
UUID.class == type ||
980978
URI.class == type ||
981979
URL.class == type ||
982980
Locale.class == type ||

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.List;
3131
import java.util.Locale;
3232
import java.util.Set;
33+
import java.util.UUID;
3334

3435
import org.junit.jupiter.api.Nested;
3536
import org.junit.jupiter.api.Test;
@@ -826,6 +827,17 @@ void caseInsensitiveValueOf() {
826827
.withMessage("Constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes");
827828
}
828829

830+
831+
private static void assertEqualHashCodes(int expected, Object array) {
832+
int actual = ObjectUtils.nullSafeHashCode(array);
833+
assertThat(actual).isEqualTo(expected);
834+
assertThat(array.hashCode()).isNotEqualTo(actual);
835+
}
836+
837+
838+
enum Tropes {FOO, BAR, baz}
839+
840+
829841
@Nested
830842
class NullSafeConciseToStringTests {
831843

@@ -887,7 +899,13 @@ void nullSafeConciseToStringForTemporal() {
887899
}
888900

889901
@Test
890-
void nullSafeConciseToStringForUri() {
902+
void nullSafeConciseToStringForUUID() {
903+
UUID id = UUID.randomUUID();
904+
assertThat(ObjectUtils.nullSafeConciseToString(id)).isEqualTo(id.toString());
905+
}
906+
907+
@Test
908+
void nullSafeConciseToStringForURI() {
891909
String uri = "https://www.example.com/?foo=1&bar=2&baz=3";
892910
assertThat(ObjectUtils.nullSafeConciseToString(URI.create(uri))).isEqualTo(uri);
893911

@@ -899,7 +917,7 @@ void nullSafeConciseToStringForUri() {
899917
}
900918

901919
@Test
902-
void nullSafeConciseToStringForUrl() throws Exception {
920+
void nullSafeConciseToStringForURL() throws Exception {
903921
String url = "https://www.example.com/?foo=1&bar=2&baz=3";
904922
assertThat(ObjectUtils.nullSafeConciseToString(new URL(url))).isEqualTo(url);
905923

@@ -959,17 +977,6 @@ private String repeat(String str, int count) {
959977
private String prefix(Class<?> clazz) {
960978
return clazz.getTypeName() + "@";
961979
}
962-
963980
}
964981

965-
966-
private static void assertEqualHashCodes(int expected, Object array) {
967-
int actual = ObjectUtils.nullSafeHashCode(array);
968-
assertThat(actual).isEqualTo(expected);
969-
assertThat(array.hashCode() != actual).isTrue();
970-
}
971-
972-
973-
enum Tropes {FOO, BAR, baz}
974-
975982
}

0 commit comments

Comments
 (0)