Skip to content

Commit 927d27b

Browse files
committed
Consider UUID as simple value type with concise toString output
Closes gh-30661
1 parent 1df5e9f commit 927d27b

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

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

Lines changed: 9 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

@@ -903,14 +904,15 @@ public static String nullSafeToString(@Nullable short[] array) {
903904
* <li>Potentially {@linkplain StringUtils#truncate(CharSequence) truncated string}
904905
* if {@code obj} is a {@link String} or {@link CharSequence}</li>
905906
* <li>Potentially {@linkplain StringUtils#truncate(CharSequence) truncated string}
906-
* if {@code obj} is a <em>simple type</em> whose {@code toString()} method returns
907-
* a non-null value.</li>
907+
* if {@code obj} is a <em>simple value type</em> whose {@code toString()} method
908+
* returns a non-null value.</li>
908909
* <li>Otherwise, a string representation of the object's type name concatenated
909910
* with {@code @} and a hex string form of the object's identity hash code</li>
910911
* </ul>
911-
* <p>In the context of this method, a <em>simple type</em> is any of the following:
912-
* a primitive wrapper (excluding {@link Void}), an {@link Enum}, a {@link Number},
913-
* a {@link Date}, a {@link Temporal}, a {@link URI}, a {@link URL}, or a {@link Locale}.
912+
* <p>In the context of this method, a <em>simple value type</em> is any of the following:
913+
* a primitive wrapper (excluding {@code Void}), an {@code Enum}, a {@code Number},
914+
* a {@code Date}, a {@code Temporal}, a {@code UUID}, a {@code URI}, a {@code URL},
915+
* or a {@code Locale}.
914916
* @param obj the object to build a string representation for
915917
* @return a concise string representation of the supplied object
916918
* @since 5.3.27
@@ -938,13 +940,7 @@ public static String nullSafeConciseToString(@Nullable Object obj) {
938940
}
939941

940942
/**
941-
* Copy of {@link org.springframework.beans.BeanUtils#isSimpleValueType(Class)}.
942-
* <p>Check if the given type represents a "simple" value type: a primitive or
943-
* primitive wrapper, an enum, a String or other CharSequence, a Number, a
944-
* Date, a Temporal, a URI, a URL, a Locale, or a Class.
945-
* <p>{@code Void} and {@code void} are not considered simple value types.
946-
* @param type the type to check
947-
* @return whether the given type represents a "simple" value type
943+
* Derived from {@link org.springframework.beans.BeanUtils#isSimpleValueType}.
948944
*/
949945
private static boolean isSimpleValueType(Class<?> type) {
950946
return (Void.class != type && void.class != type &&
@@ -954,6 +950,7 @@ private static boolean isSimpleValueType(Class<?> type) {
954950
Number.class.isAssignableFrom(type) ||
955951
Date.class.isAssignableFrom(type) ||
956952
Temporal.class.isAssignableFrom(type) ||
953+
UUID.class.isAssignableFrom(type) ||
957954
URI.class == type ||
958955
URL.class == type ||
959956
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
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.Locale;
3131
import java.util.Set;
32+
import java.util.UUID;
3233

3334
import org.junit.jupiter.api.Nested;
3435
import org.junit.jupiter.api.Test;
@@ -800,6 +801,17 @@ void caseInsensitiveValueOf() {
800801
.withMessage("Constant [bogus] does not exist in enum type org.springframework.util.ObjectUtilsTests$Tropes");
801802
}
802803

804+
805+
private static void assertEqualHashCodes(int expected, Object array) {
806+
int actual = ObjectUtils.nullSafeHashCode(array);
807+
assertThat(actual).isEqualTo(expected);
808+
assertThat(array.hashCode()).isNotEqualTo(actual);
809+
}
810+
811+
812+
enum Tropes {FOO, BAR, baz}
813+
814+
803815
@Nested
804816
class NullSafeConciseToStringTests {
805817

@@ -861,7 +873,13 @@ void nullSafeConciseToStringForTemporal() {
861873
}
862874

863875
@Test
864-
void nullSafeConciseToStringForUri() {
876+
void nullSafeConciseToStringForUUID() {
877+
UUID id = UUID.randomUUID();
878+
assertThat(ObjectUtils.nullSafeConciseToString(id)).isEqualTo(id.toString());
879+
}
880+
881+
@Test
882+
void nullSafeConciseToStringForURI() {
865883
String uri = "https://www.example.com/?foo=1&bar=2&baz=3";
866884
assertThat(ObjectUtils.nullSafeConciseToString(URI.create(uri))).isEqualTo(uri);
867885

@@ -873,7 +891,7 @@ void nullSafeConciseToStringForUri() {
873891
}
874892

875893
@Test
876-
void nullSafeConciseToStringForUrl() throws Exception {
894+
void nullSafeConciseToStringForURL() throws Exception {
877895
String url = "https://www.example.com/?foo=1&bar=2&baz=3";
878896
assertThat(ObjectUtils.nullSafeConciseToString(new URL(url))).isEqualTo(url);
879897

@@ -925,17 +943,6 @@ public String toString() {
925943
private static String prefix(Class<?> clazz) {
926944
return clazz.getTypeName() + "@";
927945
}
928-
929-
}
930-
931-
932-
private static void assertEqualHashCodes(int expected, Object array) {
933-
int actual = ObjectUtils.nullSafeHashCode(array);
934-
assertThat(actual).isEqualTo(expected);
935-
assertThat(array.hashCode()).isNotEqualTo(actual);
936946
}
937947

938-
939-
enum Tropes {FOO, BAR, baz}
940-
941948
}

0 commit comments

Comments
 (0)