Retain protype in DataContainerCodecContext
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / NotificationCodecContext.java
index 6a44af8c417e57dea4aaf0800b89aa7dc76f2453..324b01f01863a48e3ffeac72c32740de9ca5587f 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
-import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.base.Verify.verifyNotNull;
 import static net.bytebuddy.implementation.bytecode.member.MethodVariableAccess.loadThis;
 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.getField;
 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.putField;
@@ -34,17 +34,18 @@ import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
 import net.bytebuddy.jar.asm.Opcodes;
 import net.bytebuddy.matcher.ElementMatchers;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader.GeneratorResult;
+import org.opendaylight.mdsal.binding.loader.BindingClassLoader.GeneratorResult;
+import org.opendaylight.mdsal.binding.runtime.api.NotificationRuntimeType;
+import org.opendaylight.yangtools.yang.binding.BaseNotification;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.EventInstantAware;
-import org.opendaylight.yangtools.yang.binding.Notification;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
-import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 
-final class NotificationCodecContext<D extends DataObject & Notification>
-        extends DataObjectCodecContext<D, NotificationDefinition> {
+final class NotificationCodecContext<D extends DataObject & BaseNotification>
+        extends DataObjectCodecContext<D, NotificationRuntimeType> {
     private static final Generic EVENT_INSTANT_AWARE = TypeDefinition.Sort.describe(EventInstantAware.class);
 
     private static final String EVENT_INSTANT_NAME;
@@ -57,25 +58,25 @@ final class NotificationCodecContext<D extends DataObject & Notification>
     }
 
     private static final Generic BB_DOCC = TypeDefinition.Sort.describe(DataObjectCodecContext.class);
-    private static final Generic BB_DNC = TypeDefinition.Sort.describe(DistinctNodeContainer.class);
+    private static final Generic BB_DCN = TypeDefinition.Sort.describe(DataContainerNode.class);
     private static final Generic BB_I = TypeDefinition.Sort.describe(Instant.class);
 
     private static final MethodType CONSTRUCTOR_TYPE = MethodType.methodType(void.class, DataObjectCodecContext.class,
-        DistinctNodeContainer.class, Instant.class);
-    private static final MethodType NOTIFICATION_TYPE = MethodType.methodType(Notification.class,
+        DataContainerNode.class, Instant.class);
+    private static final MethodType NOTIFICATION_TYPE = MethodType.methodType(BaseNotification.class,
         NotificationCodecContext.class, ContainerNode.class, Instant.class);
     private static final String INSTANT_FIELD = "instant";
 
     private final MethodHandle eventProxy;
 
-    NotificationCodecContext(final Class<?> key, final NotificationDefinition schema,
+    NotificationCodecContext(final Class<?> notificationClass, final NotificationRuntimeType type,
             final CodecContextFactory factory) {
-        super(DataContainerCodecPrototype.from(key, schema, factory));
-        final Class<D> bindingClass = getBindingClass();
+        super(new Prototype<>(notificationClass, type, factory));
+        final var bindingClass = getBindingClass();
 
-        final Class<?> awareClass = factory().getLoader().generateClass(bindingClass, "eventInstantAware",
-            (loader, fqcn, bindingInterface) -> {
-                final Class<?> codecImpl = loader.getGeneratedClass(bindingClass, "codecImpl");
+        final var eventAwareClass = CodecPackage.EVENT_AWARE.generateClass(prototype().contextFactory().getLoader(),
+            bindingClass, (loader, fqcn, bindingInterface) -> {
+                final var codecImpl = CodecPackage.CODEC.getGeneratedClass(loader, bindingClass);
 
                 return GeneratorResult.of(new ByteBuddy()
                     .subclass(codecImpl, ConstructorStrategy.Default.NO_CONSTRUCTORS)
@@ -83,7 +84,7 @@ final class NotificationCodecContext<D extends DataObject & Notification>
                     .name(fqcn)
                     .defineField(INSTANT_FIELD, BB_I, Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC)
                     .defineConstructor(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC)
-                        .withParameters(BB_DOCC, BB_DNC, BB_I)
+                        .withParameters(BB_DOCC, BB_DCN, BB_I)
                         .intercept(ConstructorImplementation.INSTANCE)
                     .defineMethod(EVENT_INSTANT_NAME, EVENT_INSTANT_RETTYPE, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC)
                         .intercept(EventInstantImplementation.INSTANCE)
@@ -92,7 +93,7 @@ final class NotificationCodecContext<D extends DataObject & Notification>
 
         final MethodHandle ctor;
         try {
-            ctor = MethodHandles.publicLookup().findConstructor(awareClass, CONSTRUCTOR_TYPE);
+            ctor = MethodHandles.publicLookup().findConstructor(eventAwareClass, CONSTRUCTOR_TYPE);
         } catch (IllegalAccessException | NoSuchMethodException e) {
             throw new LinkageError("Failed to acquire constructor", e);
         }
@@ -101,18 +102,19 @@ final class NotificationCodecContext<D extends DataObject & Notification>
 
     @Override
     public D deserialize(final NormalizedNode data) {
-        checkState(data instanceof ContainerNode, "Unexpected data %s", data);
-        return createBindingProxy((ContainerNode) data);
+        return createBindingProxy(checkDataArgument(ContainerNode.class, data));
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")
-    Notification deserialize(final @NonNull ContainerNode data, final @NonNull Instant eventInstant) {
+    @NonNull BaseNotification deserialize(final @NonNull ContainerNode data, final @NonNull Instant eventInstant) {
+        final BaseNotification ret;
         try {
-            return (Notification) eventProxy.invokeExact(this, data, eventInstant);
+            ret = (BaseNotification) eventProxy.invokeExact(this, data, eventInstant);
         } catch (final Throwable e) {
             Throwables.throwIfUnchecked(e);
-            throw new IllegalStateException(e);
+            throw new LinkageError("Failed to instantiate notification", e);
         }
+        return verifyNotNull(ret);
     }
 
     @Override
@@ -120,6 +122,22 @@ final class NotificationCodecContext<D extends DataObject & Notification>
         return deserialize(normalizedNode);
     }
 
+    /**
+     * Prototype for a {@code notification}. This class only exists because DataContainerCodecContext requires a
+     * prototype.
+     */
+    static final class Prototype<D extends DataObject & BaseNotification>
+            extends DataObjectCodecPrototype<NotificationRuntimeType> {
+        private Prototype(final Class<?> cls, final NotificationRuntimeType type, final CodecContextFactory factory) {
+            super(cls, NodeIdentifier.create(type.statement().argument()), type, factory);
+        }
+
+        @Override
+        NotificationCodecContext<?> createInstance() {
+            throw new UnsupportedOperationException("Should never be invoked");
+        }
+    }
+
     private enum ConstructorImplementation implements Implementation {
         INSTANCE;
 
@@ -129,8 +147,8 @@ final class NotificationCodecContext<D extends DataObject & Notification>
         static {
             try {
                 LOAD_CTOR_ARGS = MethodVariableAccess.allArgumentsOf(new MethodDescription.ForLoadedConstructor(
-                    AugmentableCodecDataObject.class.getDeclaredConstructor(DataObjectCodecContext.class,
-                        DistinctNodeContainer.class)));
+                    AugmentableCodecDataObject.class.getDeclaredConstructor(AbstractDataObjectCodecContext.class,
+                        DataContainerNode.class)));
             } catch (NoSuchMethodException e) {
                 throw new ExceptionInInitializerError(e);
             }
@@ -144,7 +162,7 @@ final class NotificationCodecContext<D extends DataObject & Notification>
         @Override
         public ByteCodeAppender appender(final Target implementationTarget) {
             final TypeDescription instrumentedType = implementationTarget.getInstrumentedType();
-            final InGenericShape superCtor = instrumentedType.getSuperClass().getDeclaredMethods()
+            final InGenericShape superCtor = verifyNotNull(instrumentedType.getSuperClass()).getDeclaredMethods()
                     .filter(ElementMatchers.isConstructor()).getOnly();
 
             return new ByteCodeAppender.Simple(