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)
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")
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;
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
+}
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;
}
}
+ /**
+ * 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);
}