*/
package org.opendaylight.mdsal.binding.generator.impl.reactor;
-import static com.google.common.base.Verify.verify;
import static com.google.common.base.Verify.verifyNotNull;
import static java.util.Objects.requireNonNull;
import com.google.common.base.MoreObjects.ToStringHelper;
-import java.util.Optional;
+import com.google.common.base.VerifyException;
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;
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;
/**
* 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);
}
/**
- * 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) {
}
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;
}
/**
*/
// FIXME: this should be a generic class argument
// FIXME: this needs a better name, but 'runtimeType' is already taken.
- abstract @Nullable Type runtimeJavaType();
+ @Nullable Type runtimeJavaType() {
+ return generatedType();
+ }
/**
* Create the externally-accessible {@link RuntimeType} view of this object. The difference between
@Override
public final boolean isAddedByUses() {
- return statement instanceof AddedByUsesAware && ((AddedByUsesAware) statement).isAddedByUses();
+ return statement instanceof AddedByUsesAware aware && aware.isAddedByUses();
}
@Override
public final boolean isAugmenting() {
- return statement instanceof CopyableNode && ((CopyableNode) statement).isAugmenting();
+ return statement instanceof CopyableNode copyable && copyable.isAugmenting();
}
/**
final @Nullable AbstractExplicitGenerator<?, ?> findLocalSchemaTreeGenerator(final QName qname) {
for (Generator child : this) {
- if (child instanceof AbstractExplicitGenerator) {
- final AbstractExplicitGenerator<?, ?> gen = (AbstractExplicitGenerator<?, ?>) child;
- final EffectiveStatement<?, ?> stmt = gen.statement();
- if (stmt instanceof SchemaTreeEffectiveStatement && qname.equals(stmt.argument())) {
- return gen;
- }
+ if (child instanceof AbstractExplicitGenerator<?, ?> gen
+ && gen.statement() instanceof SchemaTreeEffectiveStatement<?> stmt && qname.equals(stmt.argument())) {
+ return gen;
}
}
return null;
}
final @NonNull QName getQName() {
- final Object arg = statement.argument();
- verify(arg instanceof QName, "Unexpected argument %s", arg);
- return (QName) arg;
+ final var arg = statement.argument();
+ if (arg instanceof QName qname) {
+ return qname;
+ }
+ throw new VerifyException("Unexpected argument " + arg);
}
@NonNull AbstractQName localName() {
// FIXME: this should be done in a nicer way
- final Object argument = statement.argument();
- verify(argument instanceof AbstractQName, "Illegal argument %s", argument);
- return (AbstractQName) argument;
+ final var arg = statement.argument();
+ if (arg instanceof AbstractQName aqn) {
+ return aqn;
+ }
+ throw new VerifyException("Illegal argument " + arg);
}
@Override
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);
}
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);
}
}
return helper;
}
+
+ static final @NonNull GeneratedType verifyGeneratedType(final Type type) {
+ if (type instanceof GeneratedType ret) {
+ return ret;
+ }
+ throw new VerifyException("Unexpected type " + type);
+ }
}