From 7ad7382a0abb95c0e6281d1a592b0cd8dd61c18d Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 4 Jun 2023 22:13:50 +0200 Subject: [PATCH] Use instanceof patterns in CodecDataObject Throw explicit VerifyExceptions instead of verify(... instanceof ...), so we make it obvious what is going on. Change-Id: I2fef6be328eb99b2f7c5e6082ad637be0374a17f Signed-off-by: Robert Varga --- .../dom/codec/impl/CodecDataObject.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObject.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObject.java index 1aa856a3de..5e7c5df0ca 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObject.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObject.java @@ -7,9 +7,9 @@ */ package org.opendaylight.mdsal.binding.dom.codec.impl; -import static com.google.common.base.Verify.verify; import static java.util.Objects.requireNonNull; +import com.google.common.base.VerifyException; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import org.eclipse.jdt.annotation.NonNull; @@ -90,8 +90,10 @@ public abstract class CodecDataObject implements DataObjec private @NonNull Object emptyObject(final @NonNull Class bindingClass) { final var childContext = context.streamChild(bindingClass); - verify(childContext instanceof NonPresenceContainerNodeCodecContext, "Unexpected context %s", childContext); - return ((NonPresenceContainerNodeCodecContext) childContext).emptyObject(); + if (childContext instanceof NonPresenceContainerNodeCodecContext nonPresence) { + return nonPresence.emptyObject(); + } + throw new VerifyException("Unexpected context " + childContext); } protected final @NonNull Object codecKey(final VarHandle handle) { @@ -124,10 +126,14 @@ public abstract class CodecDataObject implements DataObjec // Helper split out of codecKey to aid its inlining private @NonNull Object loadKey(final VarHandle handle) { - verify(data instanceof MapEntryNode, "Unsupported value %s", data); - verify(context instanceof KeyedListNodeCodecContext, "Unexpected context %s", context); - final Object obj = ((KeyedListNodeCodecContext) context) - .deserialize(((MapEntryNode) data).name()); + if (!(data instanceof MapEntryNode mapEntry)) { + throw new VerifyException("Unsupported value " + data); + } + if (!(context instanceof KeyedListNodeCodecContext listContext)) { + throw new VerifyException("Unexpected context " + context); + } + + final Object obj = listContext.deserialize(mapEntry.name()); // key is known to be non-null, no need to mask it final Object witness = handle.compareAndExchangeRelease(this, null, obj); return witness == null ? obj : witness; -- 2.36.6