ObjectUtils uses HexFormat to format byte[]

Also remove equivalent, applied temporarily in FieldError in 6.2.x.

Closes gh-35675
This commit is contained in:
rstoyanchev 2025-10-24 12:58:02 +01:00
parent af86b30aa7
commit a982c9b481
3 changed files with 8 additions and 23 deletions

View File

@ -16,8 +16,6 @@
package org.springframework.validation;
import java.util.HexFormat;
import org.jspecify.annotations.Nullable;
import org.springframework.util.Assert;
@ -127,18 +125,8 @@ public class FieldError extends ObjectError {
// We would preferably use ObjectUtils.nullSafeConciseToString(rejectedValue) here but
// keep including the full nullSafeToString representation for backwards compatibility.
return "Field error in object '" + getObjectName() + "' on field '" + this.field +
"': rejected value [" + formatRejectedValue() + "]; " +
"': rejected value [" + ObjectUtils.nullSafeToString(this.rejectedValue) + "]; " +
resolvableToString();
}
private String formatRejectedValue() {
// Special handling of byte[], to be moved into ObjectUtils in 7.0
if (this.rejectedValue instanceof byte[] bytes && bytes.length != 0) {
return "{" + HexFormat.of().formatHex(bytes) + "}";
}
return ObjectUtils.nullSafeToString(this.rejectedValue);
}
}

View File

@ -21,6 +21,7 @@ import java.nio.charset.Charset;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Collection;
import java.util.HexFormat;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -695,9 +696,9 @@ public abstract class ObjectUtils {
/**
* Return a String representation of the contents of the specified array.
* <p>The String representation consists of a list of the array's elements,
* enclosed in curly braces ({@code "{}"}). Adjacent elements are separated
* by the characters {@code ", "} (a comma followed by a space).
* <p>As of 7.0, the String representation is a hex-encoded string enclosed
* in curly braces ({@code "{}"}). The String consists of 2 hexadecimal
* chars per element, and without a delimiter between adjacent elements.
* Returns a {@code "null"} String if {@code array} is {@code null}.
* @param array the array to build a String representation for
* @return a String representation of {@code array}
@ -709,11 +710,7 @@ public abstract class ObjectUtils {
if (array.length == 0) {
return EMPTY_ARRAY;
}
StringJoiner stringJoiner = new StringJoiner(ARRAY_ELEMENT_SEPARATOR, ARRAY_START, ARRAY_END);
for (byte b : array) {
stringJoiner.add(String.valueOf(b));
}
return stringJoiner.toString();
return ARRAY_START + HexFormat.of().formatHex(array) + ARRAY_END;
}
/**

View File

@ -630,8 +630,8 @@ class ObjectUtilsTests {
@Test
void nullSafeToStringWithByteArray() {
byte[] array = {5, 8};
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{5, 8}");
byte[] array = {5, 38};
assertThat(ObjectUtils.nullSafeToString(array)).isEqualTo("{0526}");
}
@Test