From: Robert Varga Date: Thu, 11 Jan 2024 15:38:56 +0000 (+0100) Subject: Separate out DataContainerPrototype X-Git-Tag: v13.0.0~41 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=c4cd9849a119f27f88523e00c49691c3d5aef7b3 Separate out DataContainerPrototype We have CommonDataObjectCodecPrototype defining strict prerequisites while providing awefully little in terms of API worth. Split out DataContainerPrototype, which exposes a typed javaClass() and the corresponding runtimeType(). JIRA: MDSAL-815 Change-Id: Icaca2603e3b946764ff9d4157ab04be95bd4dde2 Signed-off-by: Robert Varga --- diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/AugmentationCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/AugmentationCodecContext.java index 2d61c85456..9401a0e2f6 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/AugmentationCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/AugmentationCodecContext.java @@ -37,8 +37,8 @@ final class AugmentationCodecContext> final DataContainerAnalysis analysis) { super(prototype, analysis); - final var bindingClass = CodecDataObjectGenerator.generate(prototype.getFactory().getLoader(), - prototype.getBindingClass(), analysis.leafContexts, analysis.daoProperties, null); + final var bindingClass = CodecDataObjectGenerator.generate(prototype.contextFactory().getLoader(), + prototype.javaClass(), analysis.leafContexts, analysis.daoProperties, null); final MethodHandle ctor; try { diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CaseCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CaseCodecContext.java index fb3a59b124..11d59e47a0 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CaseCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CaseCodecContext.java @@ -19,7 +19,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; final class CaseCodecContext extends DataObjectCodecContext { CaseCodecContext(final CaseCodecPrototype prototype) { - super(prototype, CodecItemFactory.of(prototype.getBindingClass())); + super(prototype, CodecItemFactory.of(prototype.javaClass())); } @Override diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ChoiceCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ChoiceCodecContext.java index a7ec5fabb2..810dfdca65 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ChoiceCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ChoiceCodecContext.java @@ -116,8 +116,8 @@ final class ChoiceCodecContext extends CommonDataObjectCod ., CommonDataObjectCodecPrototype>build(); // Load case statements valid in this choice and keep track of their names - final var choiceType = prototype.getType(); - final var factory = prototype.getFactory(); + final var choiceType = prototype.runtimeType(); + final var factory = prototype.contextFactory(); final var localCases = new HashSet(); for (var caseType : choiceType.validCaseChildren()) { @SuppressWarnings("unchecked") @@ -148,7 +148,7 @@ final class ChoiceCodecContext extends CommonDataObjectCod if (cases.size() != 1) { // Sort all possibilities by their FQCN to retain semi-predictable results final var list = new ArrayList<>(entry.getValue()); - list.sort(Comparator.comparing(proto -> proto.getBindingClass().getCanonicalName())); + list.sort(Comparator.comparing(proto -> proto.javaClass().getCanonicalName())); ambiguousByCaseBuilder.putAll(entry.getKey(), list); } else { unambiguousByCaseBuilder.put(entry.getKey(), cases.iterator().next()); @@ -282,8 +282,8 @@ final class ChoiceCodecContext extends CommonDataObjectCod Ambiguous reference {} to child of {} resolved to {}, the first case in {} This mapping is \ not guaranteed to be stable and is subject to variations based on runtime circumstances. \ Please see the stack trace for hints about the source of ambiguity.""", - type, bindingArg(), result.getBindingClass(), - Lists.transform(inexact, CommonDataObjectCodecPrototype::getBindingClass), new Throwable()); + type, bindingArg(), result.javaClass(), + Lists.transform(inexact, CommonDataObjectCodecPrototype::javaClass), new Throwable()); } } } diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecContext.java index 5c5fa04e63..cae761a9bb 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecContext.java @@ -26,24 +26,24 @@ abstract sealed class CommonDataObjectCodecContext prototype; CommonDataObjectCodecContext(final CommonDataObjectCodecPrototype prototype) { - super(prototype.getType()); + super(prototype.runtimeType()); this.prototype = requireNonNull(prototype); } @SuppressWarnings("unchecked") @Override public final Class getBindingClass() { - return Class.class.cast(prototype.getBindingClass()); + return Class.class.cast(prototype.javaClass()); } @Override protected final CodecContextFactory factory() { - return prototype.getFactory(); + return prototype.contextFactory(); } @Override protected final T type() { - return prototype.getType(); + return prototype.runtimeType(); } @Override diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecPrototype.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecPrototype.java index ec3a2ea64c..49b863b006 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecPrototype.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CommonDataObjectCodecPrototype.java @@ -11,31 +11,27 @@ import static java.util.Objects.requireNonNull; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType; +import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -abstract sealed class CommonDataObjectCodecPrototype - extends LazyCodecContextSupplier> +/** + * Common superclass for {@link DataObjectCodecPrototype} and {@link AugmentationCodecPrototype}. + * + * @param {@link CompositeRuntimeType} type + */ +abstract sealed class CommonDataObjectCodecPrototype + extends DataContainerPrototype, R> permits AugmentationCodecPrototype, DataObjectCodecPrototype { - private final @NonNull T type; - private final @NonNull CodecContextFactory factory; private final @NonNull Item bindingArg; - CommonDataObjectCodecPrototype(final Item bindingArg, final T type, final CodecContextFactory factory) { + CommonDataObjectCodecPrototype(final Item bindingArg, final R runtimeType, final CodecContextFactory factory) { + super(factory, runtimeType); this.bindingArg = requireNonNull(bindingArg); - this.type = requireNonNull(type); - this.factory = requireNonNull(factory); - } - - final @NonNull T getType() { - return type; - } - - final @NonNull CodecContextFactory getFactory() { - return factory; } - final @NonNull Class getBindingClass() { + @Override + final Class javaClass() { return bindingArg.getType(); } diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerAnalysis.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerAnalysis.java index f6cf8b6276..cbb3a97419 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerAnalysis.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerAnalysis.java @@ -52,7 +52,7 @@ final class DataContainerAnalysis { final @NonNull ImmutableMap, PropertyInfo> daoProperties; DataContainerAnalysis(final CommonDataObjectCodecPrototype prototype, final CodecItemFactory itemFactory) { - this(prototype.getBindingClass(), prototype.getType(), prototype.getFactory(), itemFactory); + this(prototype.javaClass(), prototype.runtimeType(), prototype.contextFactory(), itemFactory); } DataContainerAnalysis(final Class bindingClass, final R runtimeType, final CodecContextFactory factory, @@ -90,12 +90,12 @@ final class DataContainerAnalysis { daoPropertiesBuilder.put(retClass, new PropertyInfo.Getter(method)); final var childProto = getChildPrototype(runtimeType, factory, itemFactory, retClass); - byStreamClassBuilder.put(childProto.getBindingClass(), childProto); + byStreamClassBuilder.put(childProto.javaClass(), childProto); byYangBuilder.put(childProto.getYangArg(), childProto); // FIXME: It really feels like we should be specializing DataContainerCodecPrototype so as to ditch // createInstance() and then we could do an instanceof check instead. - if (childProto.getType() instanceof ChoiceRuntimeType) { + if (childProto.runtimeType() instanceof ChoiceRuntimeType) { final var choice = (ChoiceCodecContext) childProto.getCodecContext(); for (var cazeChild : choice.getCaseChildrenClasses()) { byBindingArgClassBuilder.put(cazeChild, childProto); diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerPrototype.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerPrototype.java new file mode 100644 index 0000000000..9fbc08ec44 --- /dev/null +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataContainerPrototype.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.mdsal.binding.dom.codec.impl; + +import static java.util.Objects.requireNonNull; + +import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType; +import org.opendaylight.yangtools.yang.binding.DataContainer; + +/** + * A prototype for codecs dealing with {@link DataContainer}s. + * + * @param {@link CodecContext} type + * @param {@link CompositeRuntimeType} type + */ +abstract sealed class DataContainerPrototype + extends LazyCodecContextSupplier permits CommonDataObjectCodecPrototype { + private final @NonNull CodecContextFactory contextFactory; + private final @NonNull R runtimeType; + + DataContainerPrototype(final CodecContextFactory contextFactory, final R runtimeType) { + this.contextFactory = requireNonNull(contextFactory); + this.runtimeType = requireNonNull(runtimeType); + } + + /** + * Return the {@link CodecContextFactory} associated with this prototype. + * + * @return the context factory associated with this prototype + */ + final @NonNull CodecContextFactory contextFactory() { + return contextFactory; + } + + /** + * Return associated run-time type. + * + * @return associated run-time type + */ + final @NonNull R runtimeType() { + return runtimeType; + } + + /** + * Return the generated binding class this prototype corresponds to. + * + * @return the generated binding class this prototype corresponds to + */ + abstract @NonNull Class javaClass(); +} diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java index 1666ec8043..a1a24dda50 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java @@ -107,7 +107,7 @@ public abstract sealed class DataObjectCodecContext possibleAugmentations; if (Augmentable.class.isAssignableFrom(bindingClass)) { // Verify we have the appropriate backing runtimeType - final var runtimeType = prototype.getType(); + final var runtimeType = prototype.runtimeType(); if (!(runtimeType instanceof AugmentableRuntimeType augmentableRuntimeType)) { throw new VerifyException( "Unexpected type %s backing augmenable %s".formatted(runtimeType, bindingClass)); @@ -138,7 +138,7 @@ public abstract sealed class DataObjectCodecContext {@link CodecContext} type */ -public abstract sealed class LazyCodecContextSupplier implements CodecContextSupplier - permits CommonDataObjectCodecPrototype { +abstract sealed class LazyCodecContextSupplier implements CodecContextSupplier + // Note: while we could merge this class into DataContainerCodecPrototype, we want to keep the lazy-loading part + // separate in case we need to non-DataContainer contexts. + permits DataContainerPrototype { private static final VarHandle INSTANCE; static { diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecContext.java index b91d8af285..9b497fce4b 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecContext.java @@ -62,7 +62,7 @@ abstract sealed class MapCodecContext, D extends DataObject & K } static @NonNull MapCodecContext of(final MapCodecPrototype prototype) { - final var bindingClass = prototype.getBindingClass(); + final var bindingClass = prototype.javaClass(); final Method keyMethod; try { keyMethod = bindingClass.getMethod(Naming.KEY_AWARE_KEY_NAME); @@ -70,8 +70,8 @@ abstract sealed class MapCodecContext, D extends DataObject & K throw new IllegalStateException("Required method not available", e); } - final var type = prototype.getType(); - final var codec = prototype.getFactory().getPathArgumentCodec(bindingClass, type); + final var type = prototype.runtimeType(); + final var codec = prototype.contextFactory().getPathArgumentCodec(bindingClass, type); return type.statement().ordering() == Ordering.SYSTEM ? new Unordered<>(prototype, keyMethod, codec) : new Ordered<>(prototype, keyMethod, codec); diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecPrototype.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecPrototype.java index 1ee41f77eb..58080631b9 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecPrototype.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/MapCodecPrototype.java @@ -19,7 +19,7 @@ import org.opendaylight.yangtools.yang.binding.KeyAware; final class MapCodecPrototype extends ListCodecPrototype { MapCodecPrototype(final Item item, final ListRuntimeType type, final CodecContextFactory factory) { super(item, type, factory); - final var clazz = getBindingClass(); + final var clazz = javaClass(); checkArgument(KeyAware.class.isAssignableFrom(clazz), "%s is not KeyAware", clazz); }