Fix InstanceIdentifier.getAncestor() implementations 94/88494/9
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 18 Mar 2020 14:10:37 +0000 (15:10 +0100)
committerRobert Varga <nite@hq.sk>
Thu, 17 Feb 2022 11:04:54 +0000 (11:04 +0000)
SpotBugs is touchy about nulls here, all of which are guaranteed
to work out due to verification logic -- but that is not quite
obvious. Fix this up by adding explicit verifies -- which do not
hurn that much.

Change-Id: I92a120c9f8d4fced2bf3e8a859772cf6d41798df
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/FixedYangInstanceIdentifier.java
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/StackedYangInstanceIdentifier.java
data/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/PathArgumentListTest.java

index bf6ba5a41310ca4b1c4467aac8325c4bfc0d16a9..3d4fd97755044e5efaa680dcf98d17ab11b54b07 100644 (file)
@@ -78,7 +78,7 @@ final class FixedYangInstanceIdentifier extends YangInstanceIdentifier implement
         }
         if (depth == path.size() - 1) {
             // Use the parent cache
-            return getParent();
+            return verifyNotNull(getParent());
         }
         return YangInstanceIdentifier.create(path.subList(0, depth));
     }
index 955ef2543eebe13c6682b91c4bc85d20b537a15d..c8ff903394400ed140e2e1d8e777ace5e4954c83 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.data.api;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Verify.verify;
+import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.Iterables;
@@ -101,7 +102,7 @@ final class StackedYangInstanceIdentifier extends YangInstanceIdentifier impleme
         final int toWalk = ourDepth - depth;
         YangInstanceIdentifier result = this;
         for (int i = 0; i < toWalk; ++i) {
-            result = result.getParent();
+            result = verifyNotNull(result.getParent());
         }
 
         return result;
index e2298784bbd6e16a4eefda9ccd770b584ad80a8c..4c08bd084f425e54a38f5abe185155762883d0a7 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.data.api;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
@@ -111,7 +112,10 @@ public class PathArgumentListTest {
         final YangInstanceIdentifier yangInstanceIdentifier1 = stackedYangInstanceIdentifier.getAncestor(4);
         assertEquals(stackedYangInstanceIdentifier, stackedYangInstanceIdentifierClone);
         assertEquals(stackedReversePathArguments, yangInstanceIdentifier1.getReversePathArguments());
+        assertSame(stackedYangInstanceIdentifier.getParent(), stackedYangInstanceIdentifier.getAncestor(3));
 
-        assertThrows(IllegalArgumentException.class, () -> stackedYangInstanceIdentifier.getAncestor(12));
+        final IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
+            () -> stackedYangInstanceIdentifier.getAncestor(12));
+        assertEquals("Depth 12 exceeds maximum depth 4", thrown.getMessage());
     }
 }