Bump byte-buddy to 1.14.11
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecDataObjectGenerator.java
index 62776c833c163554762bbc0229b6f8c88d0166c2..19f31386fa760760d15b3d1af6b8f6dcf7ab37c7 100644 (file)
@@ -41,13 +41,12 @@ import net.bytebuddy.implementation.bytecode.member.MethodReturn;
 import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
 import net.bytebuddy.jar.asm.Opcodes;
 import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.CodecContextSupplierProvider;
 import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.LocalNameProvider;
-import org.opendaylight.mdsal.binding.dom.codec.impl.ClassGeneratorBridge.NodeContextSupplierProvider;
 import org.opendaylight.mdsal.binding.loader.BindingClassLoader;
 import org.opendaylight.mdsal.binding.loader.BindingClassLoader.ClassGenerator;
 import org.opendaylight.mdsal.binding.loader.BindingClassLoader.GeneratorResult;
-import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
-import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -59,7 +58,7 @@ import org.slf4j.LoggerFactory;
  * strong connection between a generated class to the extent possible. In most cases (grouping-generated types) this
  * involves one level of indirection, which is a safe approach. If we are dealing with a type generated outside of a
  * grouping statement, though, we are guaranteed instantiation-invariance and hence can hard-wire to a runtime-constant
- * {@link NodeContextSupplier} -- which  provides significant boost to JITs ability to optimize code -- especially with
+ * {@link CodecContextSupplier} -- which  provides significant boost to JITs ability to optimize code -- especially with
  * inlining and constant propagation.
  *
  * <p>
@@ -133,14 +132,14 @@ import org.slf4j.LoggerFactory;
  * and class loading (in {@link BindingClassLoader}). The process is performed in four steps:
  * <ul>
  * <li>During code generation, the context fields are pointed towards
- *     {@link ClassGeneratorBridge#resolveNodeContextSupplier(String)} and
+ *     {@link ClassGeneratorBridge#resolveCodecContextSupplier(String)} and
  *     {@link ClassGeneratorBridge#resolveKey(String)} methods, which are public and static, hence perfectly usable
  *     in the context of a class initializer.</li>
  * <li>During class loading of generated byte code, the original instance of the generator is called to wrap the actual
  *     class loading operation. At this point the generator installs itself as the current generator for this thread via
  *     {@link ClassGeneratorBridge#setup(CodecDataObjectGenerator)} and allows the class to be loaded.
  * <li>After the class has been loaded, but before the call returns, we will force the class to initialize, at which
- *     point the static invocations will be redirected to {@link #resolveNodeContextSupplier(String)} and
+ *     point the static invocations will be redirected to {@link #resolveCodecContextSupplier(String)} and
  *     {@link #resolveKey(String)} methods, thus initializing the fields to the intended constants.</li>
  * <li>Before returning from the class loading call, the generator will detach itself via
  *     {@link ClassGeneratorBridge#tearDown(CodecDataObjectGenerator)}.</li>
@@ -155,10 +154,10 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     // FIXME: MDSAL-443: wire this implementation, which requires that BindingRuntimeTypes provides information about
     //                   types being generated from within a grouping
     private static final class Fixed<T extends CodecDataObject<?>> extends CodecDataObjectGenerator<T>
-            implements NodeContextSupplierProvider<T> {
-        private final ImmutableMap<Method, NodeContextSupplier> properties;
+            implements CodecContextSupplierProvider<T> {
+        private final ImmutableMap<Method, CodecContextSupplier> properties;
 
-        Fixed(final TypeDescription superClass, final ImmutableMap<Method, NodeContextSupplier> properties,
+        Fixed(final TypeDescription superClass, final ImmutableMap<Method, CodecContextSupplier> properties,
                 final @Nullable Method keyMethod) {
             super(superClass, keyMethod);
             this.properties = requireNonNull(properties);
@@ -170,7 +169,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             for (Method method : properties.keySet()) {
                 LOG.trace("Generating for fixed method {}", method);
                 final String methodName = method.getName();
-                final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
+                final TypeDescription retType = ForLoadedType.of(method.getReturnType());
                 tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
                     new SupplierGetterMethodImplementation(methodName, retType));
             }
@@ -178,8 +177,8 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         }
 
         @Override
-        public NodeContextSupplier resolveNodeContextSupplier(final String methodName) {
-            final Optional<Entry<Method, NodeContextSupplier>> found = properties.entrySet().stream()
+        public CodecContextSupplier resolveCodecContextSupplier(final String methodName) {
+            final Optional<Entry<Method, CodecContextSupplier>> found = properties.entrySet().stream()
                     .filter(entry -> methodName.equals(entry.getKey().getName())).findAny();
             verify(found.isPresent(), "Failed to find property for %s in %s", methodName, this);
             return verifyNotNull(found.orElseThrow().getValue());
@@ -205,7 +204,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             for (Method method : simpleProperties.keySet()) {
                 LOG.trace("Generating for simple method {}", method);
                 final String methodName = method.getName();
-                final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
+                final TypeDescription retType = ForLoadedType.of(method.getReturnType());
                 tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
                     new SimpleGetterMethodImplementation(methodName, retType));
             }
@@ -214,7 +213,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
                 final Method method = info.getterMethod();
                 LOG.trace("Generating for structured method {}", method);
                 final String methodName = method.getName();
-                final TypeDescription retType = TypeDescription.ForLoadedType.of(method.getReturnType());
+                final TypeDescription retType = ForLoadedType.of(method.getReturnType());
                 tmp = tmp.defineMethod(methodName, retType, PUB_FINAL).intercept(
                         new StructuredGetterMethodImplementation(methodName, retType, entry.getKey()));
 
@@ -260,18 +259,17 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         this.keyMethod = keyMethod;
     }
 
-    static <D extends DataObject, T extends CodecDataObject<T>> Class<T> generate(final BindingClassLoader loader,
-            final Class<D> bindingInterface, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
+    static <T extends CodecDataObject<T>> Class<T> generate(final BindingClassLoader loader,
+            final Class<?> bindingInterface, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
             final Map<Class<?>, PropertyInfo> daoProperties, final Method keyMethod) {
-        return loader.generateClass(bindingInterface, "codecImpl",
+        return CodecPackage.CODEC.generateClass(loader, bindingInterface,
             new Reusable<>(BB_CDO, simpleProperties, daoProperties, keyMethod));
     }
 
-    static <D extends DataObject, T extends CodecDataObject<T>> Class<T> generateAugmentable(
-            final BindingClassLoader loader, final Class<D> bindingInterface,
-            final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
+    static <T extends CodecDataObject<T>> Class<T> generateAugmentable(final BindingClassLoader loader,
+            final Class<?> bindingInterface, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
             final Map<Class<?>, PropertyInfo> daoProperties, final Method keyMethod) {
-        return loader.generateClass(bindingInterface, "codecImpl",
+        return CodecPackage.CODEC.generateClass(loader, bindingInterface,
             new Reusable<>(BB_ACDO, simpleProperties, daoProperties, keyMethod));
     }
 
@@ -290,7 +288,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         if (keyMethod != null) {
             LOG.trace("Generating for key {}", keyMethod);
             final String methodName = keyMethod.getName();
-            final TypeDescription retType = TypeDescription.ForLoadedType.of(keyMethod.getReturnType());
+            final TypeDescription retType = ForLoadedType.of(keyMethod.getReturnType());
             builder = builder.defineMethod(methodName, retType, PUB_FINAL).intercept(
                 new KeyMethodImplementation(methodName, retType));
         }
@@ -316,7 +314,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         return new Implementation.Simple(
             // return Foo.bindingHashCode(this);
             loadThis(),
-            invokeMethod(bindingInterface, BindingMapping.BINDING_HASHCODE_NAME, bindingInterface),
+            invokeMethod(bindingInterface, Naming.BINDING_HASHCODE_NAME, bindingInterface),
             MethodReturn.INTEGER);
     }
 
@@ -325,7 +323,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             // return Foo.bindingEquals(this, obj);
             loadThis(),
             FIRST_ARG_REF,
-            invokeMethod(bindingInterface, BindingMapping.BINDING_EQUALS_NAME, bindingInterface, Object.class),
+            invokeMethod(bindingInterface, Naming.BINDING_EQUALS_NAME, bindingInterface, Object.class),
             MethodReturn.INTEGER);
     }
 
@@ -333,7 +331,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         return new Implementation.Simple(
             // return Foo.bindingToString(this);
             loadThis(),
-            invokeMethod(bindingInterface, BindingMapping.BINDING_TO_STRING_NAME, bindingInterface),
+            invokeMethod(bindingInterface, Naming.BINDING_TO_STRING_NAME, bindingInterface),
             MethodReturn.REFERENCE);
     }
 
@@ -351,7 +349,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     private abstract static class AbstractCachedMethodImplementation extends AbstractMethodImplementation {
         private static final Generic BB_HANDLE = TypeDefinition.Sort.describe(VarHandle.class);
         private static final Generic BB_OBJECT = TypeDefinition.Sort.describe(Object.class);
-        private static final StackManipulation OBJECT_CLASS = ClassConstant.of(TypeDescription.OBJECT);
+        private static final StackManipulation OBJECT_CLASS = ClassConstant.of(ForLoadedType.of(Object.class));
         private static final StackManipulation LOOKUP = invokeMethod(MethodHandles.class, "lookup");
         private static final StackManipulation FIND_VAR_HANDLE = invokeMethod(Lookup.class,
             "findVarHandle", Class.class, String.class, Class.class);
@@ -518,10 +516,10 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
 
     private static final class SupplierGetterMethodImplementation extends AbstractCachedMethodImplementation {
         private static final StackManipulation CODEC_MEMBER = invokeMethod(CodecDataObject.class,
-            "codecMember", VarHandle.class, NodeContextSupplier.class);
+            "codecMember", VarHandle.class, CodecContextSupplier.class);
         private static final StackManipulation BRIDGE_RESOLVE = invokeMethod(ClassGeneratorBridge.class,
             "resolveNodeContextSupplier", String.class);
-        private static final Generic BB_NCS = TypeDefinition.Sort.describe(NodeContextSupplier.class);
+        private static final Generic BB_NCS = TypeDefinition.Sort.describe(CodecContextSupplier.class);
 
         // getFoo$$$C
         private final String contextName;