Clean up CodecDataObjectGenerator documentation
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / CodecDataObjectGenerator.java
index d8f1f367249269a5bed7e3ae5d12132de1eb2ac4..c965a087a93b2bd05bec8a32b7305064893118df 100644 (file)
@@ -35,6 +35,7 @@ import net.bytebuddy.description.field.FieldDescription;
 import net.bytebuddy.description.method.MethodDescription;
 import net.bytebuddy.description.type.TypeDefinition;
 import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.description.type.TypeDescription.ForLoadedType;
 import net.bytebuddy.description.type.TypeDescription.Generic;
 import net.bytebuddy.dynamic.DynamicType.Builder;
 import net.bytebuddy.dynamic.scaffold.InstrumentedType;
@@ -108,8 +109,10 @@ import org.slf4j.LoggerFactory;
  * This strategy minimizes the bytecode footprint and follows the generally good idea of keeping common logic in a
  * single place in a maintainable form. The glue code is extremely light (~6 instructions), which is beneficial on both
  * sides of invocation:
- * - generated method can readily be inlined into the caller
- * - it forms a call site into which codeMember() can be inlined with VarHandle being constant
+ * <ul>
+ *   <li>generated method can readily be inlined into the caller</li>
+ *   <li>it forms a call site into which codeMember() can be inlined with VarHandle being constant</li>
+ * </ul>
  *
  * <p>
  * The second point is important here, as it allows the invocation logic around VarHandle to completely disappear,
@@ -130,12 +133,12 @@ import org.slf4j.LoggerFactory;
  * <p>
  * The sticky point here is the NodeContextSupplier, as it is a heap object which cannot normally be looked up from the
  * static context in which the static class initializer operates -- so we need perform some sort of a trick here.
- * Eventhough ByteBuddy provides facilities for bridging references to type fields, those facilities operate on volatile
- * fields -- hence they do not quite work for us.
+ * Even though ByteBuddy provides facilities for bridging references to type fields, those facilities operate on
+ * volatile fields -- hence they do not quite work for us.
  *
  * <p>
  * Another alternative, which we used in Javassist-generated DataObjectSerializers, is to muck with the static field
- * using reflection -- which works, but requires redefinition of Field.modifiers, which is something Java 9 complains
+ * using reflection -- which works, but requires redefinition of Field.modifiers, which is something Java 9+ complains
  * about quite noisily.
  *
  * <p>
@@ -150,7 +153,7 @@ import org.slf4j.LoggerFactory;
  *     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 redirect to {@link #resolveNodeContextSupplier(String)} and
+ *     point the static invocations will be redirected to {@link #resolveNodeContextSupplier(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>
@@ -163,14 +166,14 @@ import org.slf4j.LoggerFactory;
 abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements ClassGenerator<T> {
     // Not reusable definition: we can inline NodeContextSuppliers without a problem
     // FIXME: 6.0.0: wire this implementation, which requires that BindingRuntimeTypes provides information about types
-    //               being genenerated from within a grouping
+    //               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;
 
-        Fixed(final Builder<?> template, final ImmutableMap<Method, NodeContextSupplier> properties,
+        Fixed(final TypeDescription superClass, final ImmutableMap<Method, NodeContextSupplier> properties,
                 final @Nullable Method keyMethod) {
-            super(template, keyMethod);
+            super(superClass, keyMethod);
             this.properties = requireNonNull(properties);
         }
 
@@ -207,9 +210,9 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         private final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties;
         private final Map<Method, Class<?>> daoProperties;
 
-        Reusable(final Builder<?> template, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
+        Reusable(final TypeDescription superClass, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
                 final Map<Method, Class<?>> daoProperties, final @Nullable Method keyMethod) {
-            super(template, keyMethod);
+            super(superClass, keyMethod);
             this.simpleProperties = requireNonNull(simpleProperties);
             this.daoProperties = requireNonNull(daoProperties);
         }
@@ -258,6 +261,8 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     private static final Generic BB_DATAOBJECT = TypeDefinition.Sort.describe(DataObject.class);
     private static final Generic BB_HELPER = TypeDefinition.Sort.describe(ToStringHelper.class);
     private static final Generic BB_INT = TypeDefinition.Sort.describe(int.class);
+    private static final TypeDescription BB_CDO = ForLoadedType.of(CodecDataObject.class);
+    private static final TypeDescription BB_ACDO = ForLoadedType.of(AugmentableCodecDataObject.class);
     private static final Comparator<Method> METHOD_BY_ALPHABET = Comparator.comparing(Method::getName);
 
     private static final StackManipulation ARRAYS_EQUALS = invokeMethod(Arrays.class, "equals",
@@ -272,20 +277,13 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     private static final int PROT_FINAL = Opcodes.ACC_PROTECTED | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC;
     private static final int PUB_FINAL = Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_SYNTHETIC;
 
-    private static final Builder<?> CDO;
-    private static final Builder<?> ACDO;
-
-    static {
-        final ByteBuddy bb = new ByteBuddy();
-        CDO = bb.subclass(CodecDataObject.class).visit(ByteBuddyUtils.computeFrames());
-        ACDO = bb.subclass(AugmentableCodecDataObject.class).visit(ByteBuddyUtils.computeFrames());
-    }
+    private static final ByteBuddy BB = new ByteBuddy();
 
-    private final Builder<?> template;
+    private final TypeDescription superClass;
     private final Method keyMethod;
 
-    CodecDataObjectGenerator(final Builder<?> template, final @Nullable Method keyMethod) {
-        this.template = requireNonNull(template);
+    CodecDataObjectGenerator(final TypeDescription superClass, final @Nullable Method keyMethod) {
+        this.superClass = requireNonNull(superClass);
         this.keyMethod = keyMethod;
     }
 
@@ -293,7 +291,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             final Class<D> bindingInterface, final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
             final Map<Method, Class<?>> daoProperties, final Method keyMethod) {
         return loader.generateClass(bindingInterface, "codecImpl",
-            new Reusable<>(CDO, simpleProperties, daoProperties, keyMethod));
+            new Reusable<>(BB_CDO, simpleProperties, daoProperties, keyMethod));
     }
 
     static <D extends DataObject, T extends CodecDataObject<T>> Class<T> generateAugmentable(
@@ -301,7 +299,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             final ImmutableMap<Method, ValueNodeCodecContext> simpleProperties,
             final Map<Method, Class<?>> daoProperties, final Method keyMethod) {
         return loader.generateClass(bindingInterface, "codecImpl",
-            new Reusable<>(ACDO, simpleProperties, daoProperties, keyMethod));
+            new Reusable<>(BB_ACDO, simpleProperties, daoProperties, keyMethod));
     }
 
     @Override
@@ -309,8 +307,10 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             final Class<?> bindingInterface) {
         LOG.trace("Generating class {}", fqcn);
 
+        final Generic bindingDef = TypeDefinition.Sort.describe(bindingInterface);
         @SuppressWarnings("unchecked")
-        Builder<T> builder = (Builder<T>) template.name(fqcn).implement(bindingInterface);
+        Builder<T> builder = (Builder<T>) BB.subclass(Generic.Builder.parameterizedType(superClass, bindingDef).build())
+            .visit(ByteBuddyUtils.computeFrames()).name(fqcn).implement(bindingDef);
 
         builder = generateGetters(builder);