Bug 4456 - StackOverflowError on recursive extension definition
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / EffectiveStatementBase.java
index eb24880ced8e42e770bda3c27585bd8e8342e261..a381200a8439fb63b3d5003715218121b7e0450e 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.ImmutableList;
@@ -27,31 +28,78 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 
 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
 
+    private static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
+            new Predicate<StmtContext<?,?,?>>() {
+        @Override
+        public boolean apply(final StmtContext<?, ?, ?> input) {
+            return input.isSupportedToBuildEffective();
+        }
+    };
+
+    private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
+            new Predicate<StmtContext<?,?,?>>() {
+        @Override
+        public boolean apply(final StmtContext<?, ?, ?> input) {
+            return StmtContextUtils.isUnknownStatement(input);
+        }
+    };
+
     private final List<? extends EffectiveStatement<?, ?>> substatements;
+    private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
 
     protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx) {
-        Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
+        this(ctx, true);
+    }
 
-        Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
+    /**
+     * Constructor.
+     *
+     * @param ctx
+     *            context of statement.
+     * @param buildUnknownSubstatements
+     *            if it is false, the unknown substatements are omitted from
+     *            build of effective substatements till the call of either
+     *            effectiveSubstatements or getOmittedUnknownSubstatements
+     *            method. The main purpose of this is to allow the build of
+     *            recursive extension definitions.
+     */
+    protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
+
+        final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
+        final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
 
         for(StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
                 substatementsInit.add(declaredSubstatement);
                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
-                ((StatementContextBase<?, ?, ?>)ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
+                ((StatementContextBase<?, ?, ?>) ctx).removeStatementsFromEffectiveSubstatements(declaredSubstatement
                         .getEffectOfStatement());
             } else {
                 substatementsInit.add(declaredSubstatement);
             }
         }
-
         substatementsInit.addAll(effectiveSubstatements);
 
-        this.substatements = ImmutableList.copyOf(Collections2.transform(
-            Collections2.filter(substatementsInit, StmtContextUtils.IS_SUPPORTED_TO_BUILD_EFFECTIVE),
+        Collection<StatementContextBase<?, ?, ?>> substatementsToBuild = Collections2.filter(substatementsInit,
+                IS_SUPPORTED_TO_BUILD_EFFECTIVE);
+        if (!buildUnknownSubstatements) {
+            this.unknownSubstatementsToBuild = ImmutableList.copyOf(Collections2.filter(substatementsToBuild,
+                    IS_UNKNOWN_STATEMENT_CONTEXT));
+            substatementsToBuild = Collections2.filter(substatementsToBuild,
+                    Predicates.not(IS_UNKNOWN_STATEMENT_CONTEXT));
+        } else {
+            this.unknownSubstatementsToBuild = ImmutableList.of();
+        }
+
+        this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild,
                 StmtContextUtils.buildEffective()));
     }
 
+    Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
+        return Collections2.transform(unknownSubstatementsToBuild,
+                StmtContextUtils.buildEffective());
+    }
+
     @Override
     public final <K, V, N extends IdentifierNamespace<K, V>> V get(final Class<N> namespace, final K identifier) {
         throw new UnsupportedOperationException("Not implemented yet.");
@@ -64,7 +112,11 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
 
     @Override
     public final Collection<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
-        return substatements;
+        if (unknownSubstatementsToBuild.isEmpty()) {
+            return substatements;
+        } else {
+            return ImmutableList.copyOf(Iterables.concat(substatements, getOmittedUnknownSubstatements()));
+        }
     }
 
     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {