Move use of EffectiveStmtCtx.caerbannog() 41/93941/3
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 26 Nov 2020 22:59:55 +0000 (23:59 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 27 Nov 2020 00:11:30 +0000 (01:11 +0100)
Is it turns out a lot of our logic can be migrated to plain
EffectiveStmtCtx.Current access, reducing the number of warnings
and potential exposure of StatementContextBase internals.

JIRA: YANGTOOLS-1186
Change-Id: Ic5dd688c75a0d0e2285b4bef9f1c77f059d09c1c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractEffectiveModule.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStmtUtils.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/action/ActionStatementSupport.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/container/AbstractContainerStatementSupport.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/list/AbstractListEffectiveStatement.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/notification/NotificationStatementRFC7950Support.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java

index ac8ef3295dcef042b3d4203503fa574226e61150..99f7be9c319468357fd005eb488503b2d602385d 100644 (file)
@@ -96,23 +96,19 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<Unqual
         final Set<UsesNode> mutableUses = new LinkedHashSet<>();
         final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
 
-        final var rabbit = stmt.caerbannog();
         for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements()) {
             if (effectiveStatement instanceof UsesNode && !mutableUses.add((UsesNode) effectiveStatement)) {
-                throw EffectiveStmtUtils.createNameCollisionSourceException(rabbit, stmt.sourceReference(),
-                        effectiveStatement);
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
             }
             if (effectiveStatement instanceof TypedefEffectiveStatement) {
                 final TypeDefinition<?> type = ((TypedefEffectiveStatement) effectiveStatement).getTypeDefinition();
                 if (!mutableTypeDefinitions.add(type)) {
-                    throw EffectiveStmtUtils.createNameCollisionSourceException(rabbit, stmt.sourceReference(),
-                            effectiveStatement);
+                    throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
                 }
             }
             if (effectiveStatement instanceof GroupingDefinition
                     && !mutableGroupings.add((GroupingDefinition) effectiveStatement)) {
-                throw EffectiveStmtUtils.createNameCollisionSourceException(rabbit, stmt.sourceReference(),
-                        effectiveStatement);
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, effectiveStatement);
             }
         }
 
@@ -245,10 +241,6 @@ public abstract class AbstractEffectiveModule<D extends DeclaredStatement<Unqual
                 .toString();
     }
 
-    protected static final @NonNull String findPrefix1(final Current<?, ?> stmt, final String type, final String name) {
-        return findPrefix(stmt.caerbannog(), type, name);
-    }
-
     protected static final @NonNull String findPrefix(final StmtContext<?, ?, ?> stmt, final String type,
             final String name) {
         return SourceException.throwIfNull(
index f093e387f3622fb9b0f29748d7a3fa9ef35bf41c..3d268767542b4a89e272e5bc8d207c93f009f549 100644 (file)
@@ -30,9 +30,8 @@ import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 
 @Beta
 public final class EffectiveStmtUtils {
@@ -43,11 +42,12 @@ public final class EffectiveStmtUtils {
         // Hidden on purpose
     }
 
-    public static SourceException createNameCollisionSourceException(final StmtContext<?, ?, ?> ctx,
-            final StatementSourceReference ref, final EffectiveStatement<?, ?> effectiveStatement) {
-        return new SourceException(ref,
+    public static SourceException createNameCollisionSourceException(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final EffectiveStatement<?, ?> effectiveStatement) {
+        return new SourceException(stmt.sourceReference(),
             "Error in module '%s': cannot add '%s'. Node name collision: '%s' already declared.",
-            ctx.getRoot().rawStatementArgument(), effectiveStatement.argument(), effectiveStatement.argument());
+            stmt.caerbannog().getRoot().rawStatementArgument(), effectiveStatement.argument(),
+            effectiveStatement.argument());
     }
 
     public static Optional<ElementCountConstraint> createElementCountConstraint(final EffectiveStatement<?, ?> stmt) {
@@ -167,34 +167,33 @@ public final class EffectiveStmtUtils {
         return false;
     }
 
-    public static void checkUniqueGroupings(final StmtContext<?, ?, ?> ctx,
-            final Collection<? extends EffectiveStatement<?, ?>> statements, final StatementSourceReference ref) {
-        checkUniqueNodes(ctx, statements, GroupingDefinition.class, ref);
+    public static void checkUniqueGroupings(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final Collection<? extends EffectiveStatement<?, ?>> statements) {
+        checkUniqueNodes(stmt, statements, GroupingDefinition.class);
     }
 
-    public static void checkUniqueTypedefs(final StmtContext<?, ?, ?> ctx,
-            final Collection<? extends EffectiveStatement<?, ?>> statements, final StatementSourceReference ref) {
+    public static void checkUniqueTypedefs(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final Collection<? extends EffectiveStatement<?, ?>> statements) {
         final Set<Object> typedefs = new HashSet<>();
-        for (EffectiveStatement<?, ?> stmt : statements) {
-            if (stmt instanceof TypedefEffectiveStatement
-                    && !typedefs.add(((TypedefEffectiveStatement) stmt).getTypeDefinition())) {
-                throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, ref, stmt);
+        for (EffectiveStatement<?, ?> statement : statements) {
+            if (statement instanceof TypedefEffectiveStatement
+                    && !typedefs.add(((TypedefEffectiveStatement) statement).getTypeDefinition())) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, statement);
             }
         }
     }
 
-    public static void checkUniqueUses(final StmtContext<?, ?, ?> ctx,
-            final Collection<? extends EffectiveStatement<?, ?>> statements, final StatementSourceReference ref) {
-        checkUniqueNodes(ctx, statements, UsesNode.class, ref);
+    public static void checkUniqueUses(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final Collection<? extends EffectiveStatement<?, ?>> statements) {
+        checkUniqueNodes(stmt, statements, UsesNode.class);
     }
 
-    private static void checkUniqueNodes(final StmtContext<?, ?, ?> ctx,
-            final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<?> type,
-            final StatementSourceReference ref) {
+    private static void checkUniqueNodes(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final Collection<? extends EffectiveStatement<?, ?>> statements, final Class<?> type) {
         final Set<Object> nodes = new HashSet<>();
-        for (EffectiveStatement<?, ?> stmt : statements) {
-            if (type.isInstance(stmt) && !nodes.add(stmt)) {
-                throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, ref, stmt);
+        for (EffectiveStatement<?, ?> statement : statements) {
+            if (type.isInstance(statement) && !nodes.add(statement)) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, statement);
             }
         }
     }
index 1d3d08a06124bf1c36a322fec59dfc6d9f40805c..dfe3f6e327a5ce7b9abda1201807967351c5a560 100644 (file)
@@ -95,15 +95,14 @@ public final class ActionStatementSupport extends
         final StatementSourceReference ref = stmt.sourceReference();
         checkState(!substatements.isEmpty(), "Missing implicit input/output statements at %s", ref);
         final QName argument = stmt.coerceArgument();
-        final var rabbit = stmt.caerbannog();
-        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(rabbit, ILLEGAL_PARENTS), ref,
+        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), ref,
             "Action %s is defined within a notification, rpc or another action", argument);
         SourceException.throwIf(
-            !StmtContextUtils.hasAncestorOfTypeWithChildOfType(rabbit, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
+            !StmtContextUtils.hasAncestorOfTypeWithChildOfType(stmt, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
             "Action %s is defined within a list that has no key statement", argument);
-        SourceException.throwIf(StmtContextUtils.hasParentOfType(rabbit, YangStmtMapping.CASE), ref,
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), ref,
             "Action %s is defined within a case statement", argument);
-        SourceException.throwIf(StmtContextUtils.hasParentOfType(rabbit, YangStmtMapping.MODULE), ref,
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.MODULE), ref,
             "Action %s is defined at the top level of a module", argument);
 
         return new ActionEffectiveStatementImpl(stmt.declared(), stmt.getSchemaPath(),
index ac6d56b95d5ed20d6bc9b1a0b03f69007b311174..cdae50b54382dee48d83ff328b3f978916b8caf8 100644 (file)
@@ -54,11 +54,9 @@ abstract class AbstractContainerStatementSupport
                 .setPresence(findFirstStatement(substatements, PresenceEffectiveStatement.class) != null)
                 .toFlags();
 
-        final var ref = stmt.sourceReference();
-        final var rabbit = stmt.caerbannog();
-        EffectiveStmtUtils.checkUniqueGroupings(rabbit, substatements, ref);
-        EffectiveStmtUtils.checkUniqueTypedefs(rabbit, substatements, ref);
-        EffectiveStmtUtils.checkUniqueUses(rabbit, substatements, ref);
+        EffectiveStmtUtils.checkUniqueGroupings(stmt, substatements);
+        EffectiveStmtUtils.checkUniqueTypedefs(stmt, substatements);
+        EffectiveStmtUtils.checkUniqueUses(stmt, substatements);
 
         return new ContainerEffectiveStatementImpl(stmt.declared(), substatements, stmt.sourceReference(), flags, path,
             original);
index 601640cae509811842eb9f9578cf75bd725f053e..1ac449c5a7c0ecbb7b695caddb5754321bde1718 100644 (file)
@@ -56,10 +56,9 @@ abstract class AbstractListEffectiveStatement
             final ImmutableList<QName> keyDefinition) {
         super(stmt.declared(), substatements, stmt.sourceReference());
 
-        final var rabbit = stmt.caerbannog();
-        EffectiveStmtUtils.checkUniqueGroupings(rabbit, substatements, stmt.sourceReference());
-        EffectiveStmtUtils.checkUniqueTypedefs(rabbit, substatements, stmt.sourceReference());
-        EffectiveStmtUtils.checkUniqueUses(rabbit, substatements, stmt.sourceReference());
+        EffectiveStmtUtils.checkUniqueGroupings(stmt, substatements);
+        EffectiveStmtUtils.checkUniqueTypedefs(stmt, substatements);
+        EffectiveStmtUtils.checkUniqueUses(stmt, substatements);
 
         this.substatements = maskList(substatements);
         this.path = requireNonNull(path);
index 52c15dadcb0f5f5dab7e8fcd6daf60be0c021fb3..0bead4a686cfd20e7dbc939096faf543b843b3d4 100644 (file)
@@ -66,13 +66,12 @@ public final class NotificationStatementRFC7950Support extends AbstractNotificat
     void checkEffective(final Current<QName, NotificationStatement> stmt) {
         final QName argument = stmt.argument();
         final StatementSourceReference ref = stmt.sourceReference();
-        final var rabbit = stmt.caerbannog();
-        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(rabbit, ILLEGAL_PARENTS), ref,
+        SourceException.throwIf(StmtContextUtils.hasAncestorOfType(stmt, ILLEGAL_PARENTS), ref,
             "Notification %s is defined within an rpc, action, or another notification", argument);
         SourceException.throwIf(
-            !StmtContextUtils.hasAncestorOfTypeWithChildOfType(rabbit, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
+            !StmtContextUtils.hasAncestorOfTypeWithChildOfType(stmt, YangStmtMapping.LIST, YangStmtMapping.KEY), ref,
             "Notification %s is defined within a list that has no key statement", argument);
-        SourceException.throwIf(StmtContextUtils.hasParentOfType(rabbit, YangStmtMapping.CASE), ref,
+        SourceException.throwIf(StmtContextUtils.hasParentOfType(stmt, YangStmtMapping.CASE), ref,
             "Notification %s is defined within a case statement", argument);
     }
 }
index aadf22441be765ab15949e8d3e7dd83d6446f7f8..941fe034276a6fd93fdee905540b27e2aaa98841 100644 (file)
@@ -34,6 +34,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UnknownStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
+import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Parent;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
@@ -335,22 +336,20 @@ public final class StmtContextUtils {
      * Checks whether at least one ancestor of a StatementContext matches one from a collection of statement
      * definitions.
      *
-     * @param ctx
-     *            StatementContext to be checked
-     * @param ancestorTypes
-     *            collection of statement definitions
+     * @param stmt EffectiveStmtCtx to be checked
+     * @param ancestorTypes collection of statement definitions
      * @return true if at least one ancestor of a StatementContext matches one
      *         from collection of statement definitions, otherwise false.
      */
-    public static boolean hasAncestorOfType(final StmtContext<?, ?, ?> ctx,
+    public static boolean hasAncestorOfType(final EffectiveStmtCtx stmt,
             final Collection<StatementDefinition> ancestorTypes) {
         requireNonNull(ancestorTypes);
-        StmtContext<?, ?, ?> current = ctx.getParentContext();
+        Parent current = stmt.parent();
         while (current != null) {
-            if (ancestorTypes.contains(current.getPublicDefinition())) {
+            if (ancestorTypes.contains(current.publicDefinition())) {
                 return true;
             }
-            current = current.getParentContext();
+            current = current.parent();
         }
         return false;
     }
@@ -358,19 +357,19 @@ public final class StmtContextUtils {
     /**
      * Checks whether all of StmtContext's ancestors of specified type have a child of specified type.
      *
-     * @param ctx StmtContext to be checked
+     * @param stmt EffectiveStmtCtx to be checked
      * @param ancestorType type of ancestor to search for
      * @param ancestorChildType type of child to search for in the specified ancestor type
      * @return true if all of StmtContext's ancestors of specified type have a child of specified type, otherwise false
      */
     public static <A, D extends DeclaredStatement<A>> boolean hasAncestorOfTypeWithChildOfType(
-            final StmtContext<?, ?, ?> ctx, final StatementDefinition ancestorType,
+            final EffectiveStmtCtx.Current<?, ?> stmt, final StatementDefinition ancestorType,
             final StatementDefinition ancestorChildType) {
-        requireNonNull(ctx);
+        requireNonNull(stmt);
         requireNonNull(ancestorType);
         requireNonNull(ancestorChildType);
 
-        StmtContext<?, ?, ?> current = ctx.coerceParentContext();
+        StmtContext<?, ?, ?> current = stmt.caerbannog().getParentContext();
         StmtContext<?, ?, ?> parent = current.getParentContext();
         while (parent != null) {
             if (ancestorType.equals(current.getPublicDefinition())
@@ -385,13 +384,23 @@ public final class StmtContextUtils {
         return true;
     }
 
+    /**
+     * Checks whether the parent of EffectiveStmtCtx is of specified type.
+     *
+     * @param stmt EffectiveStmtCtx to be checked
+     * @param parentType type of parent to check
+     * @return true if the parent of StmtContext is of specified type, otherwise false
+     */
+    public static boolean hasParentOfType(final EffectiveStmtCtx.Current<?, ?> stmt,
+            final StatementDefinition parentType) {
+        return hasParentOfType(stmt.caerbannog(), parentType);
+    }
+
     /**
      * Checks whether the parent of StmtContext is of specified type.
      *
-     * @param ctx
-     *            StmtContext to be checked
-     * @param parentType
-     *            type of parent to check
+     * @param ctx StmtContext to be checked
+     * @param parentType type of parent to check
      * @return true if the parent of StmtContext is of specified type, otherwise false
      */
     public static boolean hasParentOfType(final StmtContext<?, ?, ?> ctx, final StatementDefinition parentType) {