Use @Nullable in AbstractExplicitGenerator.runtimeType()
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractExplicitGenerator.java
index f593ef5b18b890e9cba73ba66970e6ce838139d3..7e3ad91645b342b847ad2053b4282ada3ebca9e8 100644 (file)
@@ -12,11 +12,11 @@ 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;
 import org.opendaylight.mdsal.binding.generator.impl.tree.StatementRepresentation;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.model.api.TypeMemberComment;
@@ -24,7 +24,7 @@ import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotableTypeBuilde
 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
 import org.opendaylight.mdsal.binding.runtime.api.RuntimeType;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
 import org.opendaylight.yangtools.yang.common.AbstractQName;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AddedByUsesAware;
@@ -38,6 +38,7 @@ import org.slf4j.LoggerFactory;
 /**
  * An explicit {@link Generator}, associated with a particular {@link EffectiveStatement}.
  */
+// FIXME: unify this with Generator
 public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?, ?>, R extends RuntimeType>
         extends Generator implements CopyableNode, StatementRepresentation<S> {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractExplicitGenerator.class);
@@ -79,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) {
@@ -92,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;
     }
 
     /**
@@ -113,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();
     }
 
     /**
@@ -266,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;
         }
@@ -275,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;
         }
@@ -309,18 +329,18 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
             return;
         }
 
-        final Type returnType = methodReturnType(builderFactory);
+        final var returnType = methodReturnType(builderFactory);
         constructGetter(builder, returnType);
         constructRequire(builder, returnType);
     }
 
     MethodSignatureBuilder constructGetter(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
-        return constructGetter(builder, returnType, BindingMapping.getGetterMethodName(localName().getLocalName()));
+        return constructGetter(builder, returnType, Naming.getGetterMethodName(localName().getLocalName()));
     }
 
     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);
 
@@ -335,7 +355,7 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
     }
 
     final void constructRequireImpl(final GeneratedTypeBuilderBase<?> builder, final Type returnType) {
-        constructGetter(builder, returnType, BindingMapping.getRequireMethodName(localName().getLocalName()))
+        constructGetter(builder, returnType, Naming.getRequireMethodName(localName().getLocalName()))
             .setDefault(true)
             .setMechanics(ValueMechanics.NONNULL);
     }
@@ -365,4 +385,11 @@ public abstract class AbstractExplicitGenerator<S extends EffectiveStatement<?,
         }
         return helper;
     }
+
+    static final @NonNull GeneratedType verifyGeneratedType(final Type type) {
+        if (type instanceof GeneratedType ret) {
+            return ret;
+        }
+        throw new VerifyException("Unexpected type " + type);
+    }
 }