BUG-6972: Do not allow root StmtContext to be copied 40/58540/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Jun 2017 17:02:49 +0000 (19:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Jun 2017 23:43:52 +0000 (01:43 +0200)
It cannot be targeted, hence it does not make sense to copy it.
Removes a chunk of duplicate code and takes this class out of the
picture where copy operations are concerned.

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

index 4bf2541ffccd9dbe9ccc3e2310b1bbef1ce365ce..a54ae2744a74c7a4ecaef51bca0e75d16a43788f 100644 (file)
@@ -116,8 +116,18 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
 
+    /**
+     * @return copy of this considering {@link CopyType} (augment, uses)
+     *
+     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
+     */
     Mutable<A, D, E> createCopy(StatementContextBase<?, ?, ?> newParent, CopyType typeOfCopy);
 
+    /**
+     * @return copy of this considering {@link CopyType} (augment, uses)
+     *
+     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
+     */
     Mutable<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent,
             CopyType typeOfCopy);
 
index 3d1bef5e2a337257c42e616f32adeb5e9532b2e1..1246b9a8357f4838d8f5dae34465c64fbb1fcd9e 100644 (file)
@@ -31,7 +31,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 
@@ -71,29 +70,6 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         this.setRootIdentifier(identifier);
     }
 
-    private RootStatementContext(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
-        final CopyType typeOfCopy) {
-        super(original);
-
-        sourceContext = Preconditions.checkNotNull(original.sourceContext);
-        this.argument = original.argument;
-
-        final Collection<? extends Mutable<?, ?, ?>> declared = original.mutableDeclaredSubstatements();
-        final Collection<? extends Mutable<?, ?, ?>> effective = original.mutableEffectiveSubstatements();
-        final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
-
-        for (final Mutable<?, ?, ?> stmtContext : declared) {
-            if (stmtContext.isSupportedByFeatures()) {
-                buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
-            }
-        }
-        for (final StmtContext<?, ?, ?> stmtContext : effective) {
-            buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
-        }
-
-        addEffectiveSubstatements(buffer);
-    }
-
     /**
      * @return null as root cannot have parent
      */
@@ -137,36 +113,16 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         return argument;
     }
 
-    /**
-     * @return copy of this considering {@link CopyType} (augment, uses)
-     *
-     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
-     */
     @Override
     public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
             final CopyType typeOfCopy) {
-        return createCopy(null, newParent, typeOfCopy);
+        throw new UnsupportedOperationException("Root context cannot be copied");
     }
 
-    /**
-     * @return copy of this considering {@link CopyType} (augment, uses)
-     *
-     * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
-     */
     @Override
     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
             final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
-        final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
-
-        copy.appendCopyHistory(typeOfCopy, this.getCopyHistory());
-
-        if (this.getOriginalCtx() != null) {
-            copy.setOriginalCtx(this.getOriginalCtx());
-        } else {
-            copy.setOriginalCtx(this);
-        }
-        definition().onStatementAdded(copy);
-        return copy;
+        throw new UnsupportedOperationException("Root context cannot be copied");
     }
 
     @Nonnull