Upstream StmtContextUtils.producesDeclared() 78/89078/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 2 Feb 2020 11:46:05 +0000 (12:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 16 Apr 2020 19:07:46 +0000 (21:07 +0200)
With Java 8 we can make this method readily available as a default,
without having to have it in a separate utils method.

Since we are at it, also introduce producesEffective(), which allows
us to infer details about the effective instance.

Also migrate open-coded equivalents to use the common method where
possible.

Change-Id: Ie2590c460296efea9fafe6a52bb3548bafe0ae0c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 61507ff3a5b8960b33cf71d90c61c53f2a8b2aff)

yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/augment/AbstractAugmentStatementSupport.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/base/BaseStatementSupport.java
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/uses/UsesStatementSupport.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java
yang/yang-parser-spi/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java

index f70283c0f7baeec8f0779784a4c6b4911854a610..994c2a5a07f43fc7d4735ad586e984a65a3b2412 100644 (file)
@@ -1049,7 +1049,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
         final Object argument = getStatementArgument();
         if (argument instanceof QName) {
             final QName qname = (QName) argument;
-            if (StmtContextUtils.producesDeclared(this, UsesStatement.class)) {
+            if (producesDeclared(UsesStatement.class)) {
                 return maybeParentPath.orElse(null);
             }
 
@@ -1062,9 +1062,8 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
             return parentPath.createChild(qname);
         }
         if (argument instanceof SchemaNodeIdentifier
-                && (StmtContextUtils.producesDeclared(this, AugmentStatement.class)
-                        || StmtContextUtils.producesDeclared(this, RefineStatement.class)
-                        || StmtContextUtils.producesDeclared(this, DeviationStatement.class))) {
+                && (producesDeclared(AugmentStatement.class) || producesDeclared(RefineStatement.class)
+                        || producesDeclared(DeviationStatement.class))) {
 
             return parentPath.createChild(((SchemaNodeIdentifier) argument).getPathFromRoot());
         }
index b51b067806e4cae1514815910c24e257357d733a..c34a126a4f3752fefb10da8c0a349958ba7c74ed 100644 (file)
@@ -204,9 +204,8 @@ abstract class AbstractAugmentStatementSupport
 
     static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
-        final CopyType typeOfCopy = UsesStatement.class.equals(sourceCtx.coerceParentContext().getPublicDefinition()
-                .getDeclaredRepresentationClass()) ? CopyType.ADDED_BY_USES_AUGMENTATION
-                : CopyType.ADDED_BY_AUGMENTATION;
+        final CopyType typeOfCopy = sourceCtx.coerceParentContext().producesDeclared(UsesStatement.class)
+                ? CopyType.ADDED_BY_USES_AUGMENTATION : CopyType.ADDED_BY_AUGMENTATION;
         /*
          * Since Yang 1.1, if an augmentation is made conditional with a
          * "when" statement, it is allowed to add mandatory nodes.
@@ -264,7 +263,7 @@ abstract class AbstractAugmentStatementSupport
             final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy,
             final boolean skipCheckOfMandatoryNodes) {
 
-        if (WhenStatement.class.equals(sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
+        if (sourceCtx.producesDeclared(WhenStatement.class)) {
             return;
         }
 
@@ -274,12 +273,9 @@ abstract class AbstractAugmentStatementSupport
         }
 
         // Data definition statements must not collide on their namespace
-        if (DataDefinitionStatement.class.isAssignableFrom(
-            sourceCtx.getPublicDefinition().getDeclaredRepresentationClass())) {
+        if (sourceCtx.producesDeclared(DataDefinitionStatement.class)) {
             for (final StmtContext<?, ?, ?> subStatement : targetCtx.allSubstatements()) {
-                if (DataDefinitionStatement.class.isAssignableFrom(
-                    subStatement.getPublicDefinition().getDeclaredRepresentationClass())) {
-
+                if (subStatement.producesDeclared(DataDefinitionStatement.class)) {
                     InferenceException.throwIf(
                         Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument()),
                         sourceCtx.getStatementSourceReference(),
index e849e7ea01ce7549cee3dfa6d417ff64fe28b48d..ea15f9d965490eb8df83e98b5ffa4214920a5445 100644 (file)
@@ -60,7 +60,7 @@ public final class BaseStatementSupport extends AbstractQNameStatementSupport<Ba
     @Override
     public void onStatementDefinitionDeclared(final Mutable<QName, BaseStatement, BaseEffectiveStatement> baseStmtCtx) {
         final Mutable<?, ?, ?> baseParentCtx = baseStmtCtx.getParentContext();
-        if (StmtContextUtils.producesDeclared(baseParentCtx, IdentityStatement.class)) {
+        if (baseParentCtx.producesDeclared(IdentityStatement.class)) {
 
             final QName baseIdentityQName = baseStmtCtx.coerceStatementArgument();
             final ModelActionBuilder baseIdentityAction = baseStmtCtx.newInferenceAction(
index 807fc494060da1909ddb629e9d4235ef09c29b79..3019ef1cd26385d73b107e8b487e443ff3a0d53e 100644 (file)
@@ -230,8 +230,7 @@ public final class UsesStatementSupport
     private static void resolveUsesNode(final Mutable<QName, UsesStatement, UsesEffectiveStatement> usesNode,
             final StmtContext<?, ?, ?> targetNodeStmtCtx) {
         for (final Mutable<?, ?, ?> subStmtCtx : usesNode.mutableDeclaredSubstatements()) {
-            if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)
-                    && areFeaturesSupported(subStmtCtx)) {
+            if (subStmtCtx.producesDeclared(RefineStatement.class) && areFeaturesSupported(subStmtCtx)) {
                 performRefine(subStmtCtx, targetNodeStmtCtx);
             }
         }
index fc10ce193fc6920ff35548230182767cf880106b..09807c0566e855de4c47d9b9ae11614f676f1836 100644 (file)
@@ -108,6 +108,15 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
     }
 
+    default <X, Y extends DeclaredStatement<X>> boolean producesDeclared(final Class<? super Y> type) {
+        return type.isAssignableFrom(getPublicDefinition().getDeclaredRepresentationClass());
+    }
+
+    default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<A, D>> boolean producesEffective(
+            final Class<? super Z> type) {
+        return type.isAssignableFrom(getPublicDefinition().getEffectiveRepresentationClass());
+    }
+
     /**
      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
      * {@link Optional#empty()} is returned.
index 7d4ffbfe0f78e86be2a100b93bfe830c70799635..8fc0cbd33fc5ae32031bf409fa91d2aa53312a6d 100644 (file)
@@ -52,7 +52,7 @@ public final class StmtContextUtils {
     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(
             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<D> declaredType) {
         for (final StmtContext<?, ?, ?> ctx : contexts) {
-            if (producesDeclared(ctx, declaredType)) {
+            if (ctx.producesDeclared(declaredType)) {
                 return (A) ctx.getStatementArgument();
             }
         }
@@ -62,7 +62,7 @@ public final class StmtContextUtils {
     @SuppressWarnings("unchecked")
     public static <A, D extends DeclaredStatement<A>> A firstAttributeOf(final StmtContext<?, ?, ?> ctx,
             final Class<D> declaredType) {
-        return producesDeclared(ctx, declaredType) ? (A) ctx.getStatementArgument() : null;
+        return ctx.producesDeclared(declaredType) ? (A) ctx.getStatementArgument() : null;
     }
 
     public static <A, D extends DeclaredStatement<A>> A firstSubstatementAttributeOf(
@@ -74,7 +74,7 @@ public final class StmtContextUtils {
     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstDeclaredSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 return (StmtContext<A, ?, ?>) subStmtContext;
             }
         }
@@ -102,7 +102,7 @@ public final class StmtContextUtils {
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
             }
         }
@@ -114,7 +114,7 @@ public final class StmtContextUtils {
             final StmtContext<?, ?, ?> stmtContext, final Class<D> type) {
         final ImmutableList.Builder<StmtContext<A, D, ?>> listBuilder = ImmutableList.builder();
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
-            if (producesDeclared(subStmtContext, type)) {
+            if (subStmtContext.producesDeclared(type)) {
                 listBuilder.add((StmtContext<A, D, ?>) subStmtContext);
             }
         }
@@ -133,7 +133,7 @@ public final class StmtContextUtils {
     public static <A, D extends DeclaredStatement<A>> StmtContext<A, ?, ?> findFirstEffectiveSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<D> declaredType) {
         for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
-            if (producesDeclared(subStmtContext, declaredType)) {
+            if (subStmtContext.producesDeclared(declaredType)) {
                 return (StmtContext<A, ?, ?>) subStmtContext;
             }
         }
@@ -193,6 +193,7 @@ public final class StmtContextUtils {
         return null;
     }
 
+    @Deprecated
     public static boolean producesDeclared(final StmtContext<?, ?, ?> ctx,
             final Class<? extends DeclaredStatement<?>> type) {
         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
@@ -238,8 +239,7 @@ public final class StmtContextUtils {
      *             if supplied statement context is null
      */
     public static boolean isUnrecognizedStatement(final StmtContext<?, ?, ?> stmtCtx) {
-        return UnrecognizedStatement.class
-                .isAssignableFrom(stmtCtx.getPublicDefinition().getDeclaredRepresentationClass());
+        return stmtCtx.producesDeclared(UnrecognizedStatement.class);
     }
 
     public static boolean checkFeatureSupport(final StmtContext<?, ?, ?> stmtContext,
@@ -586,9 +586,9 @@ public final class StmtContextUtils {
         final StmtContext<?, ?, ?> rootCtx = ctx.getRoot();
         final QNameModule qnameModule;
 
-        if (producesDeclared(rootCtx, ModuleStatement.class)) {
+        if (rootCtx.producesDeclared(ModuleStatement.class)) {
             qnameModule = rootCtx.getFromNamespace(ModuleCtxToModuleQName.class, rootCtx);
-        } else if (producesDeclared(rootCtx, SubmoduleStatement.class)) {
+        } else if (rootCtx.producesDeclared(SubmoduleStatement.class)) {
             final String belongsToModuleName = firstAttributeOf(rootCtx.declaredSubstatements(),
                 BelongsToStatement.class);
             qnameModule = rootCtx.getFromNamespace(ModuleNameToModuleQName.class, belongsToModuleName);
@@ -608,8 +608,9 @@ public final class StmtContextUtils {
             return qnameModule;
         }
 
-        if (producesDeclared(ctx.getRoot(), SubmoduleStatement.class)) {
-            final String moduleName = ctx.getRoot().getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
+        final StmtContext<?, ?, ?> root = ctx.getRoot();
+        if (root.producesDeclared(SubmoduleStatement.class)) {
+            final String moduleName = root.getFromNamespace(BelongsToPrefixToModuleName.class, prefix);
             return ctx.getFromNamespace(ModuleNameToModuleQName.class, moduleName);
         }
 
@@ -631,8 +632,7 @@ public final class StmtContextUtils {
     public static Optional<Revision> getLatestRevision(final Iterable<? extends StmtContext<?, ?, ?>> subStmts) {
         Revision revision = null;
         for (final StmtContext<?, ?, ?> subStmt : subStmts) {
-            if (subStmt.getPublicDefinition().getDeclaredRepresentationClass().isAssignableFrom(
-                    RevisionStatement.class)) {
+            if (subStmt.producesDeclared(RevisionStatement.class)) {
                 if (revision == null && subStmt.getStatementArgument() != null) {
                     revision = (Revision) subStmt.getStatementArgument();
                 } else {