Fix Spotbugs SE_TRANSIENT_FIELD_NOT_RESTORED issues

[ERROR] Medium: The field
org.apache.commons.lang3.builder.DiffBuilder$SDiff.leftSupplier is
transient but isn't set by deserialization
[org.apache.commons.lang3.builder.DiffBuilder$SDiff] In DiffBuilder.java
SE_TRANSIENT_FIELD_NOT_RESTORED
[ERROR] Medium: The field
org.apache.commons.lang3.builder.DiffBuilder$SDiff.rightSupplier is
transient but isn't set by deserialization
[org.apache.commons.lang3.builder.DiffBuilder$SDiff] In DiffBuilder.java
SE_TRANSIENT_FIELD_NOT_RESTORED
This commit is contained in:
Gary Gregory 2025-01-09 15:29:10 -05:00
parent 744a8c30c6
commit bdcc5a0502
1 changed files with 14 additions and 5 deletions

View File

@ -164,13 +164,13 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
}
}
private static final class SDiff<T, S extends Supplier<T> & Serializable> extends Diff<T> {
private static final class SDiff<T> extends Diff<T> {
private static final long serialVersionUID = 1L;
private final transient S leftSupplier;
private final transient S rightSupplier;
private final SerializableSupplier<T> leftSupplier;
private final SerializableSupplier<T> rightSupplier;
private SDiff(final String fieldName, final S leftSupplier, final S rightSupplier, final Class<T> type) {
private SDiff(final String fieldName, final SerializableSupplier<T> leftSupplier, final SerializableSupplier<T> rightSupplier, final Class<T> type) {
super(fieldName, type);
this.leftSupplier = Objects.requireNonNull(leftSupplier);
this.rightSupplier = Objects.requireNonNull(rightSupplier);
@ -188,6 +188,15 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
}
/**
* Private interface while we still have to support serialization.
*
* @param <T> the type of results supplied by this supplier.
*/
private interface SerializableSupplier<T> extends Supplier<T>, Serializable {
// empty
}
static final String TO_STRING_FORMAT = "%s differs from %s";
/**
@ -264,7 +273,7 @@ public class DiffBuilder<T> implements Builder<DiffResult<T>> {
this.equals = testObjectsEquals && Objects.equals(left, right);
}
private <F, S extends Supplier<F> & Serializable> DiffBuilder<T> add(final String fieldName, final S left, final S right, final Class<F> type) {
private <F> DiffBuilder<T> add(final String fieldName, final SerializableSupplier<F> left, final SerializableSupplier<F> right, final Class<F> type) {
diffs.add(new SDiff<>(fieldName, left, right, type));
return this;
}