Skip to content

Commit a982c9b

Browse files
committed
ObjectUtils uses HexFormat to format byte[]
Also remove equivalent, applied temporarily in FieldError in 6.2.x. Closes gh-35675
1 parent af86b30 commit a982c9b

File tree

3 files changed

+8
-23
lines changed

3 files changed

+8
-23
lines changed

spring-context/src/main/java/org/springframework/validation/FieldError.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.validation;
1818

19-
import java.util.HexFormat;
20-
2119
import org.jspecify.annotations.Nullable;
2220

2321
import org.springframework.util.Assert;
@@ -127,18 +125,8 @@ public String toString() {
127125
// We would preferably use ObjectUtils.nullSafeConciseToString(rejectedValue) here but
128126
// keep including the full nullSafeToString representation for backwards compatibility.
129127
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
130-
"': rejected value [" + formatRejectedValue() + "]; " +
128+
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
131129
resolvableToString();
132130
}
133131

134-
private String formatRejectedValue() {
135-
136-
// Special handling of byte[], to be moved into ObjectUtils in 7.0
137-
if (this.rejectedValue instanceof byte[] bytes && bytes.length != 0) {
138-
return "{" + HexFormat.of().formatHex(bytes) + "}";
139-
}
140-
141-
return ObjectUtils.nullSafeToString(this.rejectedValue);
142-
}
143-
144132
}

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.time.ZoneId;
2222
import java.util.Arrays;
2323
import java.util.Collection;
24+
import java.util.HexFormat;
2425
import java.util.Map;
2526
import java.util.Objects;
2627
import java.util.Optional;
@@ -695,9 +696,9 @@ public static String nullSafeToString(boolean @Nullable [] array) {
695696

696697
/**
697698
* Return a String representation of the contents of the specified array.
698-
* <p>The String representation consists of a list of the array's elements,
699-
* enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
700-
* by the characters {@code ", "} (a comma followed by a space).
699+
* <p>As of 7.0, the String representation is a hex-encoded string enclosed
700+
* in curly braces ({@code "{}"}). The String consists of 2 hexadecimal
701+
* chars per element, and without a delimiter between adjacent elements.
701702
* Returns a {@code "null"} String if {@code array} is {@code null}.
702703
* @param array the array to build a String representation for
703704
* @return a String representation of {@code array}
@@ -709,11 +710,7 @@ public static String nullSafeToString(byte @Nullable [] array) {
709710
if (array.length == 0) {
710711
return EMPTY_ARRAY;
711712
}
712-
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
713-
for (byte b : array) {
714-
stringJoiner.add(String.valueOf(b));
715-
}
716-
return stringJoiner.toString();
713+
return ARRAY_START + HexFormat.of().formatHex(array) + ARRAY_END;
717714
}
718715

719716
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ void nullSafeToStringWithBooleanArrayEqualToNull() {
630630

631631
@Test
632632
void nullSafeToStringWithByteArray() {
633-
byte[] array = {5, 8};
634-
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{5, 8}");
633+
byte[] array = {5, 38};
634+
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{0526}");
635635
}
636636

637637
@Test

0 commit comments

Comments
 (0)