Integrate {Inference,Source}Exception with CommonStmtCtx
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / EffectiveStmtUtils.java
index d3fec27761bab12cfe021862af5318cbedff65e7..b29ed882faebbef22b3bf8a5866b5f968aa15311 100644 (file)
@@ -16,7 +16,6 @@ import java.util.Iterator;
 import java.util.Optional;
 import java.util.Set;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
@@ -31,7 +30,7 @@ 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;
 
 @Beta
@@ -43,13 +42,11 @@ public final class EffectiveStmtUtils {
         // Hidden on purpose
     }
 
-    public static SourceException createNameCollisionSourceException(final StmtContext<?, ?, ?> ctx,
+    public static SourceException createNameCollisionSourceException(final EffectiveStmtCtx.Current<?, ?> stmt,
             final EffectiveStatement<?, ?> effectiveStatement) {
-        return new SourceException(ctx.getStatementSourceReference(),
+        return new SourceException(stmt,
             "Error in module '%s': cannot add '%s'. Node name collision: '%s' already declared.",
-            ctx.getRoot().getStatementArgument(),
-            effectiveStatement.argument(),
-            effectiveStatement.argument());
+            stmt.root().rawArgument(), effectiveStatement.argument(), effectiveStatement.argument());
     }
 
     public static Optional<ElementCountConstraint> createElementCountConstraint(final EffectiveStatement<?, ?> stmt) {
@@ -143,8 +140,8 @@ public final class EffectiveStmtUtils {
         while (iter.hasNext() && !defaultValues.isEmpty()) {
             final EffectiveStatement<?, ?> effectiveSubstatement = iter.next();
             if (YangStmtMapping.BIT.equals(effectiveSubstatement.statementDefinition())) {
-                final QName bitQName = (QName) effectiveSubstatement.argument();
-                if (defaultValues.remove(bitQName.getLocalName()) && containsIfFeature(effectiveSubstatement)) {
+                final String bitName = (String) effectiveSubstatement.argument();
+                if (defaultValues.remove(bitName) && containsIfFeature(effectiveSubstatement)) {
                     return true;
                 }
             } else if (YangStmtMapping.ENUM.equals(effectiveSubstatement.statementDefinition())
@@ -169,33 +166,33 @@ public final class EffectiveStmtUtils {
         return false;
     }
 
-    public static void checkUniqueGroupings(final StmtContext<?, ?, ?> ctx,
+    public static void checkUniqueGroupings(final EffectiveStmtCtx.Current<?, ?> stmt,
             final Collection<? extends EffectiveStatement<?, ?>> statements) {
-        checkUniqueNodes(ctx, statements, GroupingDefinition.class);
+        checkUniqueNodes(stmt, statements, GroupingDefinition.class);
     }
 
-    public static void checkUniqueTypedefs(final StmtContext<?, ?, ?> ctx,
+    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, 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,
+    public static void checkUniqueUses(final EffectiveStmtCtx.Current<?, ?> stmt,
             final Collection<? extends EffectiveStatement<?, ?>> statements) {
-        checkUniqueNodes(ctx, statements, UsesNode.class);
+        checkUniqueNodes(stmt, statements, UsesNode.class);
     }
 
-    private static void checkUniqueNodes(final StmtContext<?, ?, ?> ctx,
+    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, stmt);
+        for (EffectiveStatement<?, ?> statement : statements) {
+            if (type.isInstance(statement) && !nodes.add(statement)) {
+                throw EffectiveStmtUtils.createNameCollisionSourceException(stmt, statement);
             }
         }
     }