Make AugmentationCodecPrototype generic 50/109750/11
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 11 Jan 2024 23:35:38 +0000 (00:35 +0100)
committerRobert Varga <nite@hq.sk>
Sat, 13 Jan 2024 15:27:01 +0000 (15:27 +0000)
Augmentations should capture their target type, which eliminates the
need for quite a few casts.

JIRA: MDSAL-815
Change-Id: I3d9c8326ac2bb24135c66ecfe240ed95cea98318
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/AugmentationCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/AugmentationCodecPrototype.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java

index d7842638aadb4a0c1020a81f8575cc05af36d647..c4250f65a36cf99a1f538057475054dc1742842b 100644 (file)
@@ -33,7 +33,7 @@ final class AugmentationCodecContext<D extends DataObject & Augmentation<?>>
 
     private final MethodHandle proxyConstructor;
 
-    private AugmentationCodecContext(final AugmentationCodecPrototype prototype,
+    private AugmentationCodecContext(final AugmentationCodecPrototype<D> prototype,
             final DataContainerAnalysis<AugmentRuntimeType> analysis) {
         super(prototype, analysis);
 
@@ -50,7 +50,7 @@ final class AugmentationCodecContext<D extends DataObject & Augmentation<?>>
         proxyConstructor = ctor.asType(DATAOBJECT_TYPE);
     }
 
-    AugmentationCodecContext(final AugmentationCodecPrototype prototype) {
+    AugmentationCodecContext(final AugmentationCodecPrototype<D> prototype) {
         this(prototype, new DataContainerAnalysis<>(prototype, CodecItemFactory.of()));
     }
 
@@ -73,7 +73,7 @@ final class AugmentationCodecContext<D extends DataObject & Augmentation<?>>
     @SuppressWarnings("checkstyle:illegalCatch")
     @Override
     public D filterFrom(final DataContainerNode parentData) {
-        for (var childArg : ((AugmentationCodecPrototype) prototype()).getChildArgs()) {
+        for (var childArg : ((AugmentationCodecPrototype<?>) prototype()).getChildArgs()) {
             if (parentData.childByArg(childArg) != null) {
                 try {
                     return (D) proxyConstructor.invokeExact(this, parentData);
index 5f10f1abc5d8e6321da7a9aa781f2eb58ff2523a..1b5ae222f661a6dbaa0cb1829635fdd329473c79 100644 (file)
@@ -12,17 +12,17 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.collect.ImmutableSet;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
-import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.NodeStep;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 
-final class AugmentationCodecPrototype extends CommonDataObjectCodecPrototype<AugmentRuntimeType> {
+final class AugmentationCodecPrototype<T extends Augmentation<?>>
+        extends CommonDataObjectCodecPrototype<AugmentRuntimeType> {
     private final @NonNull ImmutableSet<NodeIdentifier> childArgs;
 
-    @SuppressWarnings("unchecked")
-    AugmentationCodecPrototype(final @NonNull Class<?> cls, final AugmentRuntimeType type,
+    AugmentationCodecPrototype(final @NonNull Class<T> cls, final AugmentRuntimeType type,
             final CodecContextFactory factory, final ImmutableSet<NodeIdentifier> childArgs) {
-        super(new NodeStep<>((Class<? extends DataObject>) cls), type, factory);
+        super(new NodeStep<>(cls), type, factory);
         this.childArgs = requireNonNull(childArgs);
     }
 
index 6ab6c0157b46b20e8de3390c698ce810ab336dee..91fe183ec45b87966ff4ea9964ecee8ae3c3cf60 100644 (file)
@@ -134,7 +134,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
 
         // Deal with augmentations, which are not something we analysis provides
         final var augPathToBinding = new HashMap<NodeIdentifier, Class<?>>();
-        final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype>();
+        final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype<?>>();
         for (var augment : possibleAugmentations) {
             final var augProto = loadAugmentPrototype(augment);
             if (augProto != null) {
@@ -187,13 +187,13 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
          * and walk all stream child and compare augmentations classes if they are equivalent. When we find a match
          * we'll cache it so we do not need to perform reflection operations again.
          */
-        final var local = (ImmutableMap<Class<?>, AugmentationCodecPrototype>) MISMATCHED_AUGMENTED.getAcquire(this);
+        final var local = (ImmutableMap<Class<?>, AugmentationCodecPrototype<?>>) MISMATCHED_AUGMENTED.getAcquire(this);
         final var mismatched = local.get(childClass);
         return mismatched != null ? mismatched : loadMismatchedAugmentation(local, childClass);
     }
 
-    private @Nullable AugmentationCodecPrototype loadMismatchedAugmentation(
-            final ImmutableMap<Class<?>, AugmentationCodecPrototype> oldMismatched,
+    private @Nullable AugmentationCodecPrototype<?> loadMismatchedAugmentation(
+            final ImmutableMap<Class<?>, AugmentationCodecPrototype<?>> oldMismatched,
             final @NonNull Class<?> childClass) {
         @SuppressWarnings("rawtypes")
         final Class<?> augTarget = findAugmentationTarget((Class) childClass);
@@ -211,9 +211,9 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
         return null;
     }
 
-    private @NonNull AugmentationCodecPrototype cacheMismatched(
-            final @NonNull ImmutableMap<Class<?>, AugmentationCodecPrototype> oldMismatched,
-            final @NonNull Class<?> childClass, final @NonNull AugmentationCodecPrototype prototype) {
+    private @NonNull AugmentationCodecPrototype<?> cacheMismatched(
+            final @NonNull ImmutableMap<Class<?>, AugmentationCodecPrototype<?>> oldMismatched,
+            final @NonNull Class<?> childClass, final @NonNull AugmentationCodecPrototype<?> prototype) {
         var expected = oldMismatched;
         while (true) {
             final var newMismatched =
@@ -222,7 +222,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
                     .put(childClass, prototype)
                     .build();
 
-            final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype>)
+            final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype<?>>)
                 MISMATCHED_AUGMENTED.compareAndExchangeRelease(this, expected, newMismatched);
             if (witness == expected) {
                 LOG.trace("Cached mismatched augmentation {} -> {} in {}", childClass, prototype, this);
@@ -250,7 +250,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
         return cls.equals(loaded);
     }
 
-    private @Nullable AugmentationCodecPrototype loadAugmentPrototype(final AugmentRuntimeType augment) {
+    private @Nullable AugmentationCodecPrototype<?> loadAugmentPrototype(final AugmentRuntimeType augment) {
         // FIXME: in face of deviations this code should be looking at declared view, i.e. all possibilities at augment
         //        declaration site
         final var childPaths = augment.statement()
@@ -271,7 +271,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
             throw new IllegalStateException(
                 "RuntimeContext references type " + javaType + " but failed to load its class", e);
         }
-        return new AugmentationCodecPrototype(augClass, augment, factory, childPaths);
+        return new AugmentationCodecPrototype<>(augClass, augment, factory, childPaths);
     }
 
     @Override