Improve StmtContext.copyAsChild() type safety 16/102516/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 30 Sep 2022 12:58:02 +0000 (14:58 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 30 Sep 2022 13:06:42 +0000 (15:06 +0200)
We should not be losing A/D/E type capture, but rather propagate it.

Change-Id: I6254a4f0ef0fea5b79815e99d1e6b03d33f679e4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/InferredStatementContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/ReplicaStatementContext.java
parser/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
parser/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java

index 243c8fd1e797e6027fbd1608ec49e5eca3ccad11..c528de4152e2843446935b4a1a68386e8477f0be 100644 (file)
@@ -460,9 +460,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         }
 
         @SuppressWarnings("unchecked")
-        final Mutable<QName, Y, Z> ret = (Mutable<QName, Y, Z>) copySubstatement((Mutable<?, ?, ?>) template)
-            .orElseThrow(
-                () -> new InferenceException(this, "Failed to materialize child %s template %s", qname, template));
+        final var ret = (Mutable<QName, Y, Z>) copySubstatement(template).orElseThrow(
+            () -> new InferenceException(this, "Failed to materialize child %s template %s", qname, template));
         addMaterialized(template, ensureCompletedPhase(ret));
 
         LOG.debug("Child {} materialized", qname);
@@ -592,7 +591,8 @@ final class InferredStatementContext<A, D extends DeclaredStatement<A>, E extend
         }
     }
 
-    private Optional<? extends Mutable<?, ?, ?>> copySubstatement(final Mutable<?, ?, ?> substatement) {
+    private <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Optional<Mutable<X, Y, Z>>
+            copySubstatement(final StmtContext<X, Y, Z> substatement) {
         return substatement.copyAsChildOf(this, childCopyType(), targetModule);
     }
 
index f7c1f4344cba7ce5f174b29af28d2a05e25bb651..9952c348d68bcf032342326504ffe220df41022c 100644 (file)
@@ -122,7 +122,7 @@ final class ReplicaStatementContext<A, D extends DeclaredStatement<A>, E extends
     }
 
     @Override
-    public Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> newParent, final CopyType type,
+    public Optional<Mutable<A, D, E>> copyAsChildOf(final Mutable<?, ?, ?> newParent, final CopyType type,
             final QNameModule targetModule) {
         return source.copyAsChildOf(newParent, type, targetModule);
     }
index c0213e1715deba9127c6d04f2e35fde5848710ce..e65c0a63294ec7e2c7116d4d6063ecefb1c535bc 100644 (file)
@@ -723,7 +723,7 @@ abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E extends
     }
 
     @Override
-    public final Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(final Mutable<?, ?, ?> parent, final CopyType type,
+    public final Optional<Mutable<A, D, E>> copyAsChildOf(final Mutable<?, ?, ?> parent, final CopyType type,
             final QNameModule targetModule) {
         checkEffectiveModelCompleted(this);
         return Optional.ofNullable(copyAsChildOfImpl(parent, type, targetModule));
index f53ca549e97db3a69a742a327391ed0c039b126f..e87fa9ac21b2d26c6eff4aa90626074b92b73c25 100644 (file)
@@ -158,7 +158,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
     @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
 
     @Beta
-    @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
+    @NonNull Optional<Mutable<A, D, E>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
             @Nullable QNameModule targetModule);
 
     ModelProcessingPhase getCompletedPhase();