Refactor DataObjectCodecContext.getBindingChildValue() 44/77644/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Nov 2018 14:01:07 +0000 (15:01 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 21 Nov 2018 11:10:19 +0000 (11:10 +0000)
Using instanceof checks is an OOP anti-pattern, which we do not
want here anyways. Generalize NodeCodecContext.defaultObject()
to return null by default and refactor decoding to rely on it,
with LeafNodeCodecContext returning whatever it has as a default
value.

Change-Id: I2b58bc7f1a16496cf6bc93fd7a33aec38f864597
JIRA: MDSAL-18
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 5376803e1c41d25f4c8e6e2ca38dc5e4d9fc18fb)

binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/LeafNodeCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/NodeCodecContext.java

index 7d01458173a6269361071325896a4786a300627f..6b2181d45848f63e9d08d234cb85f6e7e13161e6 100644 (file)
@@ -366,13 +366,10 @@ abstract class DataObjectCodecContext<D extends DataObject, T extends DataNodeCo
         final NodeCodecContext<?> childContext = byMethod.get(method).get();
         @SuppressWarnings("unchecked")
         final java.util.Optional<NormalizedNode<?, ?>> domChild = domData.getChild(childContext.getDomPathArgument());
-        if (domChild.isPresent()) {
-            return childContext.deserializeObject(domChild.get());
-        } else if (childContext instanceof LeafNodeCodecContext) {
-            return ((LeafNodeCodecContext)childContext).defaultObject();
-        } else {
-            return null;
-        }
+
+        // We do not want to use Optional.map() here because we do not want to invoke defaultObject() when we have
+        // normal value because defaultObject() may end up throwing an exception intentionally.
+        return domChild.isPresent() ? childContext.deserializeObject(domChild.get()) : childContext.defaultObject();
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")
index 3df83dd09e9e162d831e43b1dbfd6744198e0900..be95ba5cb28ce53c0034195f4cec63a3756f543f 100644 (file)
@@ -15,7 +15,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Set;
-import javax.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
 import org.opendaylight.yangtools.concepts.Codec;
@@ -228,12 +227,8 @@ final class LeafNodeCodecContext<D extends DataObject> extends NodeCodecContext<
         return ChildAddressabilitySummary.UNADDRESSABLE;
     }
 
-    /**
-     * Return the default value object.
-     *
-     * @return The default value object, or null if the default value is not defined.
-     */
-    @Nullable Object defaultObject() {
+    @Override
+    Object defaultObject() {
         return defaultObject;
     }
-}
\ No newline at end of file
+}
index 3311ae59de63a11fb1fe65a7e3e6d05cf9267d1c..868aa3a804b12f65b64e57ca3f133c8612fe9315 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.mdsal.binding.dom.codec.impl;
 
 import com.google.common.collect.ImmutableMap;
 import java.util.List;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
 import org.opendaylight.yangtools.concepts.Codec;
@@ -94,5 +95,15 @@ abstract class NodeCodecContext<D extends DataObject> implements BindingCodecTre
         }
     }
 
+    /**
+     * Return the default value object. Implementations of this method are explicitly allowed to throw unchecked
+     * exceptions, which are propagated as-is upwards the stack.
+     *
+     * @return The default value object, or null if the default value is not defined.
+     */
+    @Nullable Object defaultObject() {
+        return null;
+    }
+
     protected abstract Object deserializeObject(NormalizedNode<?, ?> normalizedNode);
 }