Skip to content

Commit

Permalink
Consider UUID as simple value type with concise toString output
Browse files Browse the repository at this point in the history
Closes gh-30661

(cherry picked from commit 927d27b)
  • Loading branch information
jhoeller committed Jun 14, 2023
1 parent 9cff2ac commit c2cc55e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
22 changes: 10 additions & 12 deletions spring-core/src/main/java/org/springframework/util/ObjectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.UUID;

import org.springframework.lang.Nullable;

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

/**
* Copy of {@link org.springframework.beans.BeanUtils#isSimpleValueType(Class)}.
* <p>Check if the given type represents a "simple" value type: a primitive or
* primitive wrapper, an enum, a String or other CharSequence, a Number, a
* Date, a Temporal, a URI, a URL, a Locale, or a Class.
* <p>{@code Void} and {@code void} are not considered simple value types.
* @param type the type to check
* @return whether the given type represents a "simple" value type
* Derived from {@link org.springframework.beans.BeanUtils#isSimpleValueType}.
* As of 5.3.28, considering {@code UUID} in addition to the bean-level check.
*/
private static boolean isSimpleValueType(Class<?> type) {
return (Void.class != type && void.class != type &&
Expand All @@ -977,6 +974,7 @@ private static boolean isSimpleValueType(Class<?> type) {
Number.class.isAssignableFrom(type) ||
Date.class.isAssignableFrom(type) ||
Temporal.class.isAssignableFrom(type) ||
UUID.class == type ||
URI.class == type ||
URL.class == type ||
Locale.class == type ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;

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


private static void assertEqualHashCodes(int expected, Object array) {
int actual = ObjectUtils.nullSafeHashCode(array);
assertThat(actual).isEqualTo(expected);
assertThat(array.hashCode()).isNotEqualTo(actual);
}


enum Tropes {FOO, BAR, baz}


@Nested
class NullSafeConciseToStringTests {

Expand Down Expand Up @@ -887,7 +899,13 @@ void nullSafeConciseToStringForTemporal() {
}

@Test
void nullSafeConciseToStringForUri() {
void nullSafeConciseToStringForUUID() {
UUID id = UUID.randomUUID();
assertThat(ObjectUtils.nullSafeConciseToString(id)).isEqualTo(id.toString());
}

@Test
void nullSafeConciseToStringForURI() {
String uri = "https://www.example.com/?foo=1&bar=2&baz=3";
assertThat(ObjectUtils.nullSafeConciseToString(URI.create(uri))).isEqualTo(uri);

Expand All @@ -899,7 +917,7 @@ void nullSafeConciseToStringForUri() {
}

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

Expand Down Expand Up @@ -959,17 +977,6 @@ private String repeat(String str, int count) {
private String prefix(Class<?> clazz) {
return clazz.getTypeName() + "@";
}

}


private static void assertEqualHashCodes(int expected, Object array) {
int actual = ObjectUtils.nullSafeHashCode(array);
assertThat(actual).isEqualTo(expected);
assertThat(array.hashCode() != actual).isTrue();
}


enum Tropes {FOO, BAR, baz}

}

0 comments on commit c2cc55e

Please sign in to comment.