Remove SuppressFBWarnings from InstanceIdentifier 40/109440/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 24 Nov 2023 15:00:17 +0000 (16:00 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Dec 2023 10:32:06 +0000 (11:32 +0100)
Define readObject()/writeObject() methods that enforce Serializable
Proxy pattern, which in turn silences SpotBugs.

Change-Id: Ic7c5f523adc5e0df7e9a2767f93fcadd3bc77c8a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 32bc2210ab6b6fcc6b7c5459d86352006250414a)

binding/yang-binding/src/main/java/org/opendaylight/yangtools/yang/binding/InstanceIdentifier.java

index 4e7e716712dafb48d7705690cf9cd1e74e6e811f..da23b8fc0c97addc043756e9ca0857e8a16a2151 100644 (file)
@@ -17,9 +17,11 @@ import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.VerifyException;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
-import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.io.ObjectStreamException;
-import java.io.Serial;
 import java.io.Serializable;
 import java.util.Collections;
 import java.util.Iterator;
@@ -64,14 +66,13 @@ import org.opendaylight.yangtools.util.HashCodeBuilder;
  */
 public class InstanceIdentifier<T extends DataObject>
         implements HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>> {
-    @Serial
+    @java.io.Serial
     private static final long serialVersionUID = 3L;
 
     /*
      * Protected to differentiate internal and external access. Internal access is required never to modify
      * the contents. References passed to outside entities have to be wrapped in an unmodifiable view.
      */
-    @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "Handled through Externalizable proxy")
     final Iterable<PathArgument> pathArguments;
 
     private final @NonNull Class<T> targetType;
@@ -416,11 +417,30 @@ public class InstanceIdentifier<T extends DataObject>
         return childIdentifier(Item.of(container));
     }
 
-    @Serial
+    @java.io.Serial
     private Object writeReplace() throws ObjectStreamException {
         return new InstanceIdentifierV3<>(this);
     }
 
+    @java.io.Serial
+    private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+        throwNSE();
+    }
+
+    @java.io.Serial
+    private void readObjectNoData() throws ObjectStreamException {
+        throwNSE();
+    }
+
+    @java.io.Serial
+    private void writeObject(final ObjectOutputStream stream) throws IOException {
+        throwNSE();
+    }
+
+    private void throwNSE() throws NotSerializableException {
+        throw new NotSerializableException(getClass().getName());
+    }
+
     /**
      * Create a builder rooted at this key.
      *
@@ -659,7 +679,7 @@ public class InstanceIdentifier<T extends DataObject>
     }
 
     private abstract static class AbstractPathArgument<T extends DataObject> implements PathArgument, Serializable {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         private final @NonNull Class<T> type;
@@ -714,7 +734,7 @@ public class InstanceIdentifier<T extends DataObject>
      * @param <T> Item type
      */
     public static class Item<T extends DataObject> extends AbstractPathArgument<T> {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         Item(final Class<T> type) {
@@ -764,7 +784,7 @@ public class InstanceIdentifier<T extends DataObject>
      */
     public static class IdentifiableItem<I extends KeyAware<T> & DataObject, T extends Key<I>>
             extends AbstractPathArgument<I> {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         private final @NonNull T key;
@@ -825,7 +845,7 @@ public class InstanceIdentifier<T extends DataObject>
 
     private static final class CaseItem<C extends ChoiceIn<?> & DataObject, T extends ChildOf<? super C>>
             extends Item<T> {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         private final Class<C> caseType;
@@ -843,7 +863,7 @@ public class InstanceIdentifier<T extends DataObject>
 
     private static final class CaseIdentifiableItem<C extends ChoiceIn<?> & DataObject,
             T extends ChildOf<? super C> & KeyAware<K>, K extends Key<T>> extends IdentifiableItem<T, K> {
-        @Serial
+        @java.io.Serial
         private static final long serialVersionUID = 1L;
 
         private final Class<C> caseType;