From 31ba731c16033c9fdabbf00fac1ff8adf6d5a6d0 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 26 Nov 2020 23:59:55 +0100 Subject: [PATCH] Move use of EffectiveStmtCtx.caerbannog() 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 --- .../rfc7950/stmt/AbstractEffectiveModule.java | 14 ++---- .../rfc7950/stmt/EffectiveStmtUtils.java | 47 +++++++++---------- .../stmt/action/ActionStatementSupport.java | 9 ++-- .../AbstractContainerStatementSupport.java | 8 ++-- .../list/AbstractListEffectiveStatement.java | 7 ++- .../NotificationStatementRFC7950Support.java | 7 ++- .../parser/spi/meta/StmtContextUtils.java | 41 +++++++++------- 7 files changed, 64 insertions(+), 69 deletions(-) diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractEffectiveModule.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractEffectiveModule.java index ac8ef3295d..99f7be9c31 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractEffectiveModule.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/AbstractEffectiveModule.java @@ -96,23 +96,19 @@ public abstract class AbstractEffectiveModule mutableUses = new LinkedHashSet<>(); final Set> 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 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( diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStmtUtils.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStmtUtils.java index f093e387f3..3d26876754 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStmtUtils.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/EffectiveStmtUtils.java @@ -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 createElementCountConstraint(final EffectiveStatement stmt) { @@ -167,34 +167,33 @@ public final class EffectiveStmtUtils { return false; } - public static void checkUniqueGroupings(final StmtContext ctx, - final Collection> statements, final StatementSourceReference ref) { - checkUniqueNodes(ctx, statements, GroupingDefinition.class, ref); + public static void checkUniqueGroupings(final EffectiveStmtCtx.Current stmt, + final Collection> statements) { + checkUniqueNodes(stmt, statements, GroupingDefinition.class); } - public static void checkUniqueTypedefs(final StmtContext ctx, - final Collection> statements, final StatementSourceReference ref) { + public static void checkUniqueTypedefs(final EffectiveStmtCtx.Current stmt, + final Collection> statements) { final Set 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> statements, final StatementSourceReference ref) { - checkUniqueNodes(ctx, statements, UsesNode.class, ref); + public static void checkUniqueUses(final EffectiveStmtCtx.Current stmt, + final Collection> statements) { + checkUniqueNodes(stmt, statements, UsesNode.class); } - private static void checkUniqueNodes(final StmtContext ctx, - final Collection> statements, final Class type, - final StatementSourceReference ref) { + private static void checkUniqueNodes(final EffectiveStmtCtx.Current stmt, + final Collection> statements, final Class type) { final Set 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); } } } diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/action/ActionStatementSupport.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/action/ActionStatementSupport.java index 1d3d08a061..dfe3f6e327 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/action/ActionStatementSupport.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/action/ActionStatementSupport.java @@ -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(), diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/container/AbstractContainerStatementSupport.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/container/AbstractContainerStatementSupport.java index ac6d56b95d..cdae50b543 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/container/AbstractContainerStatementSupport.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/container/AbstractContainerStatementSupport.java @@ -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); diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/list/AbstractListEffectiveStatement.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/list/AbstractListEffectiveStatement.java index 601640cae5..1ac449c5a7 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/list/AbstractListEffectiveStatement.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/list/AbstractListEffectiveStatement.java @@ -56,10 +56,9 @@ abstract class AbstractListEffectiveStatement final ImmutableList 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); diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/notification/NotificationStatementRFC7950Support.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/notification/NotificationStatementRFC7950Support.java index 52c15dadcb..0bead4a686 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/notification/NotificationStatementRFC7950Support.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/notification/NotificationStatementRFC7950Support.java @@ -66,13 +66,12 @@ public final class NotificationStatementRFC7950Support extends AbstractNotificat void checkEffective(final Current 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); } } diff --git a/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java b/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java index aadf22441b..941fe03427 100644 --- a/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java +++ b/yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java @@ -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 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 > 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) { -- 2.36.6