Use Module.asEffectiveStatement()
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / GeneratorReactor.java
index 8a8feed0995d528301bd64b6918bccf652a027ce..2907f0e2324be93feb2c6a1c1cbb6f1639abf6dd 100644 (file)
@@ -34,6 +34,7 @@ import org.opendaylight.yangtools.yang.binding.ChoiceIn;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.PathExpression;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
@@ -71,6 +72,7 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
     private State state = State.INITIALIZED;
 
     public GeneratorReactor(final EffectiveModelContext context) {
+        super(context);
         inferenceStack = SchemaInferenceStack.of(context);
 
         // Construct modules and their subtrees. Dependency sort is very much needed here, as it establishes order of
@@ -78,10 +80,8 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
         // AugmentGenerators without having forward references.
         // FIXME: migrate to new ModuleDependencySort when it is available, which streamline things here
         children = ModuleDependencySort.sort(context.getModules()).stream()
-            .map(module -> {
-                verify(module instanceof ModuleEffectiveStatement, "Unexpected module %s", module);
-                return new ModuleGenerator((ModuleEffectiveStatement) module);
-            })
+            .map(Module::asEffectiveStatement)
+            .map(ModuleGenerator::new)
             .collect(Collectors.toUnmodifiableList());
         generators = Maps.uniqueIndex(children, gen -> gen.statement().localQNameModule());
     }
@@ -135,8 +135,8 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
         final var augments = new ArrayList<AugmentRequirement>();
         for (ModuleGenerator module : children) {
             for (Generator gen : module) {
-                if (gen instanceof ModuleAugmentGenerator) {
-                    augments.add(((ModuleAugmentGenerator) gen).startLinkage(this));
+                if (gen instanceof ModuleAugmentGenerator moduleGen) {
+                    augments.add(moduleGen.startLinkage(this));
                 }
             }
         }
@@ -145,9 +145,9 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
         }
         LOG.trace("Processing linkage of {} augment generators", augments.size());
 
-        // Step 1c: ... finally establish linkage along the reverse uses/augment axis. This is needed to route generated
-        //          type manifestations (isAddedByUses/isAugmenting) to their type generation sites. Since generator
-        //          tree iteration order does not match dependencies, we may need to perform multiple passes.
+        // Step 1c: Establish linkage along the reverse uses/augment axis. This is needed to route generated type
+        //          manifestations (isAddedByUses/isAugmenting) to their type generation sites. Since generator tree
+        //          iteration order does not match dependencies, we may need to perform multiple passes.
         for (ModuleGenerator module : children) {
             verify(module.linkOriginalGenerator(), "Module %s failed to link", module);
         }
@@ -244,14 +244,14 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
         for (Generator gen : parent) {
             gen.ensureMember();
             collectCollisionDomains(result, gen);
-            if (gen instanceof AbstractCompositeGenerator) {
-                result.add(((AbstractCompositeGenerator<?>) gen).domain());
+            if (gen instanceof AbstractCompositeGenerator<?, ?> compositeGen) {
+                result.add(compositeGen.domain());
             }
         }
     }
 
     @Override
-    <E extends EffectiveStatement<QName, ?>, G extends AbstractExplicitGenerator<E>> G resolveTreeScoped(
+    <E extends EffectiveStatement<QName, ?>, G extends AbstractExplicitGenerator<E, ?>> G resolveTreeScoped(
             final Class<G> type, final QName argument) {
         LOG.trace("Searching for tree-scoped argument {} at {}", argument, stack);
 
@@ -297,7 +297,7 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
     }
 
     @Override
-    AbstractTypeObjectGenerator<?> resolveLeafref(final PathExpression path) {
+    AbstractTypeObjectGenerator<?, ?> resolveLeafref(final PathExpression path) {
         LOG.trace("Resolving path {}", path);
         verify(inferenceStack.isEmpty(), "Unexpected data tree state %s", inferenceStack);
         try {
@@ -319,7 +319,7 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
         }
     }
 
-    private @NonNull AbstractTypeAwareGenerator<?> strictResolvePath(final @NonNull PathExpression path) {
+    private @NonNull AbstractTypeAwareGenerator<?, ?, ?> strictResolvePath(final @NonNull PathExpression path) {
         try {
             inferenceStack.resolvePathExpression(path);
         } catch (IllegalArgumentException e) {
@@ -328,7 +328,7 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
         return mapToGenerator();
     }
 
-    private @Nullable AbstractTypeAwareGenerator<?> lenientResolveLeafref(final @NonNull PathExpression path) {
+    private @Nullable AbstractTypeAwareGenerator<?, ?, ?> lenientResolveLeafref(final @NonNull PathExpression path) {
         try {
             inferenceStack.resolvePathExpression(path);
         } catch (IllegalArgumentException e) {
@@ -339,7 +339,7 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
     }
 
     // Map a statement to the corresponding generator
-    private @NonNull AbstractTypeAwareGenerator<?> mapToGenerator() {
+    private @NonNull AbstractTypeAwareGenerator<?, ?, ?> mapToGenerator() {
         // Some preliminaries first: we need to be in the correct module to walk the path
         final ModuleEffectiveStatement module = inferenceStack.currentModule();
         final ModuleGenerator gen = verifyNotNull(generators.get(module.localQNameModule()),
@@ -347,9 +347,9 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
 
         // Now kick of the search
         final List<EffectiveStatement<?, ?>> stmtPath = inferenceStack.toInference().statementPath();
-        final AbstractExplicitGenerator<?> found = gen.findGenerator(stmtPath);
-        if (found instanceof AbstractTypeAwareGenerator) {
-            return (AbstractTypeAwareGenerator<?>) found;
+        final AbstractExplicitGenerator<?, ?> found = gen.findGenerator(stmtPath);
+        if (found instanceof AbstractTypeAwareGenerator<?, ?, ?> typeAware) {
+            return typeAware;
         }
         throw new VerifyException("Statements " + stmtPath + " resulted in unexpected " + found);
     }
@@ -357,9 +357,8 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
     // Note: unlike other methods, this method pushes matching child to the stack
     private void linkUsesDependencies(final Iterable<? extends Generator> parent) {
         for (Generator child : parent) {
-            if (child instanceof AbstractCompositeGenerator) {
-                LOG.trace("Visiting composite {}", child);
-                final AbstractCompositeGenerator<?> composite = (AbstractCompositeGenerator<?>) child;
+            if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
+                LOG.trace("Visiting composite {}", composite);
                 stack.push(composite);
                 composite.linkUsesDependencies(this);
                 linkUsesDependencies(composite);
@@ -394,8 +393,8 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
 
     private void linkDependencies(final Iterable<? extends Generator> parent) {
         for (Generator child : parent) {
-            if (child instanceof AbstractDependentGenerator) {
-                ((AbstractDependentGenerator<?>) child).linkDependencies(this);
+            if (child instanceof AbstractDependentGenerator<?, ?> dependent) {
+                dependent.linkDependencies(this);
             } else if (child instanceof AbstractCompositeGenerator) {
                 stack.push(child);
                 linkDependencies(child);
@@ -407,8 +406,8 @@ public final class GeneratorReactor extends GeneratorContext implements Mutable
     private void bindTypeDefinition(final Iterable<? extends Generator> parent) {
         for (Generator child : parent) {
             stack.push(child);
-            if (child instanceof AbstractTypeObjectGenerator) {
-                ((AbstractTypeObjectGenerator<?>) child).bindTypeDefinition(this);
+            if (child instanceof AbstractTypeObjectGenerator<?, ?> typeObject) {
+                typeObject.bindTypeDefinition(this);
             } else if (child instanceof AbstractCompositeGenerator) {
                 bindTypeDefinition(child);
             }