From bdcc5a050201d9bba2f723e1fcf45fe42d84451e Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Thu, 9 Jan 2025 15:29:10 -0500 Subject: [PATCH] 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 --- .../commons/lang3/builder/DiffBuilder.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java index ab358e266..cd4b433ab 100644 --- a/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java +++ b/src/main/java/org/apache/commons/lang3/builder/DiffBuilder.java @@ -164,13 +164,13 @@ public class DiffBuilder implements Builder> { } } - private static final class SDiff & Serializable> extends Diff { + private static final class SDiff extends Diff { private static final long serialVersionUID = 1L; - private final transient S leftSupplier; - private final transient S rightSupplier; + private final SerializableSupplier leftSupplier; + private final SerializableSupplier rightSupplier; - private SDiff(final String fieldName, final S leftSupplier, final S rightSupplier, final Class type) { + private SDiff(final String fieldName, final SerializableSupplier leftSupplier, final SerializableSupplier rightSupplier, final Class type) { super(fieldName, type); this.leftSupplier = Objects.requireNonNull(leftSupplier); this.rightSupplier = Objects.requireNonNull(rightSupplier); @@ -188,6 +188,15 @@ public class DiffBuilder implements Builder> { } + /** + * Private interface while we still have to support serialization. + * + * @param the type of results supplied by this supplier. + */ + private interface SerializableSupplier extends Supplier, Serializable { + // empty + } + static final String TO_STRING_FORMAT = "%s differs from %s"; /** @@ -264,7 +273,7 @@ public class DiffBuilder implements Builder> { this.equals = testObjectsEquals && Objects.equals(left, right); } - private & Serializable> DiffBuilder add(final String fieldName, final S left, final S right, final Class type) { + private DiffBuilder add(final String fieldName, final SerializableSupplier left, final SerializableSupplier right, final Class type) { diffs.add(new SDiff<>(fieldName, left, right, type)); return this; }