Use @Nullable in AbstractExplicitGenerator.runtimeType() 58/109358/7
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 16 Dec 2023 20:41:36 +0000 (21:41 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 17 Dec 2023 16:42:15 +0000 (17:42 +0100)
Rather than using Optional, split the two user groups to those who check
for null and who assume non-null.

The former continue to call runtimeType() and latter defer to
getRuntimeType().

Change-Id: I0818d806204007127a956f7598267735c90faca9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/BindingRuntimeTypesFactory.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/DefaultBindingGenerator.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/AbstractExplicitGenerator.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/AbstractTypeObjectGenerator.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/Generator.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/ListGenerator.java
binding/mdsal-binding-generator/src/main/java/org/opendaylight/mdsal/binding/generator/impl/reactor/ModuleGenerator.java

index 9a88b9a0cc9a083327a2a60b6788a2b9e3d21e90..8d28821b532e1febe233272966b5de3ea8a291b0 100644 (file)
@@ -55,8 +55,8 @@ final class BindingRuntimeTypesFactory implements Mutable {
     static @NonNull BindingRuntimeTypes createTypes(final @NonNull EffectiveModelContext context) {
         final var moduleGens = new GeneratorReactor(context).execute(TypeBuilderFactory.runtime());
 
-        final Stopwatch sw = Stopwatch.createStarted();
-        final BindingRuntimeTypesFactory factory = new BindingRuntimeTypesFactory();
+        final var sw = Stopwatch.createStarted();
+        final var factory = new BindingRuntimeTypesFactory();
         factory.indexModules(moduleGens);
         LOG.debug("Indexed {} generators in {}", moduleGens.size(), sw);
 
@@ -69,14 +69,12 @@ final class BindingRuntimeTypesFactory implements Mutable {
             final var modGen = entry.getValue();
 
             // index the module's runtime type
-            safePut(modules, "modules", entry.getKey(), modGen.runtimeType().orElseThrow());
+            safePut(modules, "modules", entry.getKey(), modGen.getRuntimeType());
 
             // index module's identities and RPC input/outputs
             for (var gen : modGen) {
                 if (gen instanceof IdentityGenerator idGen) {
-                    idGen.runtimeType().ifPresent(identity -> {
-                        safePut(identities, "identities", identity.statement().argument(), identity);
-                    });
+                    safePut(identities, "identities", idGen.statement().argument(), idGen.getRuntimeType());
                 }
             }
         }
@@ -85,18 +83,18 @@ final class BindingRuntimeTypesFactory implements Mutable {
     }
 
     private void indexRuntimeTypes(final Iterable<? extends Generator> generators) {
-        for (Generator gen : generators) {
-            if (gen instanceof AbstractExplicitGenerator<?, ?> explicitGen && gen.generatedType().isPresent()) {
-                final var type = explicitGen.runtimeType().orElseThrow();
-                if (type.javaType() instanceof GeneratedType genType) {
+        for (var gen : generators) {
+            if (gen instanceof AbstractExplicitGenerator<?, ?> explicit) {
+                final var type = explicit.generatedRuntimeType();
+                if (type != null && type.javaType() instanceof GeneratedType genType) {
                     final var name = genType.getIdentifier();
                     final var prev = allTypes.put(name, type);
                     verify(prev == null || prev == type, "Conflict on runtime type mapping of %s between %s and %s",
                         name, prev, type);
 
                     // Global indexing of cases generated for a particular choice. We look at the Generated type
-                    // and make assumptions about its shape -- which works just fine without touching the
-                    // ChoiceRuntimeType for cases.
+                    // and make assumptions about its shape -- which works just fine without touching
+                    // the ChoiceRuntimeType for cases.
                     if (type instanceof CaseRuntimeType caseType) {
                         final var ifaces = genType.getImplements();
                         // The appropriate choice and DataObject at the very least. The choice interface is the first
index 5e7b6bf407b927765b5c1c4d6dfd2d0a837181c4..c79892a484f27ef30a6ca90ad06988a1beeb8a96 100644 (file)
@@ -11,20 +11,17 @@ import com.google.common.annotations.VisibleForTesting;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import java.util.Set;
 import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 import org.kohsuke.MetaInfServices;
 import org.opendaylight.mdsal.binding.generator.BindingGenerator;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.Generator;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.GeneratorReactor;
-import org.opendaylight.mdsal.binding.generator.impl.reactor.ModuleGenerator;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 
 /**
  * Default implementation of {@link BindingGenerator}.
@@ -61,11 +58,11 @@ public final class DefaultBindingGenerator implements BindingGenerator {
     @VisibleForTesting
     static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context,
             final Collection<? extends Module> modules) {
-        final Set<ModuleEffectiveStatement> filter = modules.stream().map(Module::asEffectiveStatement)
+        final var filter = modules.stream().map(Module::asEffectiveStatement)
             .collect(Collectors.toUnmodifiableSet());
 
-        final List<GeneratedType> result = new ArrayList<>();
-        for (ModuleGenerator gen : new GeneratorReactor(context).execute(TypeBuilderFactory.codegen()).values()) {
+        final var result = new ArrayList<GeneratedType>();
+        for (var gen : new GeneratorReactor(context).execute(TypeBuilderFactory.codegen()).values()) {
             if (filter.contains(gen.statement())) {
                 addTypes(result, gen);
             }
@@ -75,11 +72,15 @@ public final class DefaultBindingGenerator implements BindingGenerator {
     }
 
     private static void addTypes(final List<GeneratedType> result, final Generator gen) {
-        gen.generatedType()
-            .filter(type -> type.getIdentifier().immediatelyEnclosingClass().isEmpty())
-            .ifPresent(result::add);
+        final var type = gen.generatedType();
+        if (type != null) {
+            if (type.getIdentifier().immediatelyEnclosingClass().isEmpty()) {
+                result.add(type);
+            }
+        }
+
         result.addAll(gen.auxiliaryGeneratedTypes());
-        for (Generator child : gen) {
+        for (var child : gen) {
             addTypes(result, child);
         }
     }
index 1ffd2928651dca6e3126aaf9d0ef35b58ddd1400..7e3ad91645b342b847ad2053b4282ada3ebca9e8 100644 (file)
@@ -12,7 +12,6 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.base.VerifyException;
-import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Member;
@@ -81,12 +80,17 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     }
 
     /**
-     * Return the {@link RuntimeType} associated with this object, if applicable. This represents the
-     * externally-accessible view of this object when considered outside the schema tree or binding tree hierarchy.
+     * Return the {@link RuntimeType} associated with this objec, if applicablet. This represents
+     * the externally-accessible view of this object when considered outside the schema tree or binding tree hierarchy.
      *
-     * @return Associated run-time type, or empty
+     * @return Associated run-time type, or {@code null}
      */
-    public final Optional<R> runtimeType() {
+    public final @Nullable R generatedRuntimeType() {
+        final var type = generatedType();
+        return type == null ? null : runtimeType();
+    }
+
+    private @Nullable R runtimeType() {
         if (!runtimeTypeInitialized) {
             final var type = runtimeJavaType();
             if (type != null) {
@@ -94,7 +98,21 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
             }
             runtimeTypeInitialized = true;
         }
-        return Optional.ofNullable(runtimeType);
+        return runtimeType;
+    }
+
+    /**
+     * Return the {@link RuntimeType} associated with this object. This represents the externally-accessible view of
+     * this object when considered outside the schema tree or binding tree hierarchy.
+     *
+     * @return Associated run-time type
+     */
+    public final @NonNull R getRuntimeType() {
+        final var local = runtimeType();
+        if (local == null) {
+            throw new VerifyException(this + " does not have a run-time type");
+        }
+        return local;
     }
 
     /**
@@ -115,7 +133,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     // FIXME: this should be a generic class argument
     // FIXME: this needs a better name, but 'runtimeType' is already taken.
     @Nullable Type runtimeJavaType() {
-        return generatedType().orElse(null);
+        return generatedType();
     }
 
     /**
@@ -268,7 +286,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     }
 
     final @NonNull QName getQName() {
-        final Object arg = statement.argument();
+        final var arg = statement.argument();
         if (arg instanceof QName qname) {
             return qname;
         }
@@ -277,7 +295,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
 
     @NonNull AbstractQName localName() {
         // FIXME: this should be done in a nicer way
-        final Object arg = statement.argument();
+        final var arg = statement.argument();
         if (arg instanceof AbstractQName aqn) {
             return aqn;
         }
@@ -311,7 +329,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
             return;
         }
 
-        final Type returnType = methodReturnType(builderFactory);
+        final var returnType = methodReturnType(builderFactory);
         constructGetter(builder, returnType);
         constructRequire(builder, returnType);
     }
@@ -322,7 +340,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
 
     final MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder,
             final Type returnType, final String methodName) {
-        final MethodSignatureBuilder getMethod = builder.addMethod(methodName).setReturnType(returnType);
+        final var getMethod = builder.addMethod(methodName).setReturnType(returnType);
 
         annotateDeprecatedIfNecessary(getMethod);
 
index 7a5849c36c7bfcc8f7b15da42e71a0ab38255e56..cf8bdad11d1f6c86bf34bb4eb71430be83c9d38a 100644 (file)
@@ -472,8 +472,8 @@ abstract class AbstractTypeObjectGenerator<S extends EffectiveStatement<?, ?>, R
             return methodReturnTypeElement;
         }
         final var genType = generatedType();
-        if (genType.isPresent()) {
-            return genType.orElseThrow();
+        if (genType != null) {
+            return genType;
         }
         final var prev = verifyNotNull(previous(), "No previous generator for %s", this);
         return prev.runtimeJavaType();
index 5158d576acadfc9b907e8209eca26038949de05e..97981adedb2ebf303a6764f2040db64504a5c32a 100644 (file)
@@ -76,8 +76,8 @@ public abstract class Generator implements Iterable<Generator> {
         this.parent = requireNonNull(parent);
     }
 
-    public final @NonNull Optional<GeneratedType> generatedType() {
-        return Optional.ofNullable(result.generatedType());
+    public final @Nullable GeneratedType generatedType() {
+        return result.generatedType();
     }
 
     public @NonNull List<GeneratedType> auxiliaryGeneratedTypes() {
index 7e1ab88bfef544a2ef791ad5950a3c528a3b150b..6d8a7728a333a9bbb36a71b51e85e615652b2656 100644 (file)
@@ -89,7 +89,7 @@ final class ListGenerator extends CompositeSchemaTreeGenerator<ListEffectiveStat
 
     private @Nullable KeyRuntimeType keyRuntimeType() {
         final var local = keyGen;
-        return local != null ? local.runtimeType().orElseThrow() : null;
+        return local != null ? local.getRuntimeType() : null;
     }
 
     @Override
index fb14fb70b5daefed49beffc5217579931d1618fd..2634d538c86bd47a0d6c176c199bc134afe151ad 100644 (file)
@@ -18,7 +18,6 @@ import org.opendaylight.mdsal.binding.generator.impl.reactor.CollisionDomain.Mem
 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultModuleRuntimeType;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
-import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
 import org.opendaylight.mdsal.binding.model.ri.BindingTypes;
 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
@@ -92,7 +91,7 @@ public final class ModuleGenerator extends AbstractCompositeGenerator<ModuleEffe
 
     @Override
     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
-        final GeneratedTypeBuilder builder = builderFactory.newGeneratedTypeBuilder(typeName());
+        final var builder = builderFactory.newGeneratedTypeBuilder(typeName());
         builder.setModuleName(statement().argument().getLocalName());
         builder.addImplementsType(BindingTypes.DATA_ROOT);
 
@@ -135,7 +134,7 @@ public final class ModuleGenerator extends AbstractCompositeGenerator<ModuleEffe
                 final var yangDataChildren = new ArrayList<YangDataRuntimeType>();
                 for (var child : ModuleGenerator.this) {
                     if (child instanceof YangDataGenerator yangDataGen) {
-                        yangDataGen.runtimeType().ifPresent(yangDataChildren::add);
+                        yangDataChildren.add(yangDataGen.getRuntimeType());
                     }
                 }