Use @Nullable in AbstractExplicitGenerator.runtimeType()
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / AbstractExplicitGenerator.java
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);