Update byte-buddy integration 06/95006/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 3 Feb 2021 09:16:53 +0000 (10:16 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 3 Feb 2021 10:07:40 +0000 (11:07 +0100)
Update ByteBuddy to 1.10.20.

Reuse ElementMatcher for isDefaultConstructor(), lowering
the amount of garbage we potentially generate. While we are at it
make sure our intra-package use patters are more consistent.

Change-Id: I5cc297129c4b04eade4d1a8a9dfb96cd8504554e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/pom.xml
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/ByteBuddyUtils.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/CodecDataObjectGenerator.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectStreamerGenerator.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/NotificationCodecContext.java

index 048187b9a2f618d39c2a3831b52fec58c0f99444..ae8aef77bfa037da72b2c38e6607080d507b4f70 100644 (file)
@@ -29,7 +29,7 @@
             <!-- We are going to shade this -->
             <groupId>net.bytebuddy</groupId>
             <artifactId>byte-buddy</artifactId>
-            <version>1.10.17</version>
+            <version>1.10.20</version>
         </dependency>
         <dependency>
             <groupId>org.opendaylight.yangtools</groupId>
index c5bdc771b5b4dd8a3bcdedb86b9cedc45df01525..7ae3ae88546952bfcda94a18bcfc63da1f1d564b 100644 (file)
@@ -33,12 +33,17 @@ import net.bytebuddy.matcher.ElementMatchers;
 import net.bytebuddy.pool.TypePool;
 
 final class ByteBuddyUtils {
-    static final StackManipulation THIS = MethodVariableAccess.loadThis();
+    private static final StackManipulation LOAD_THIS = MethodVariableAccess.loadThis();
 
     private ByteBuddyUtils() {
 
     }
 
+    // TODO: eliminate this method once MethodVariableAccess.loadThis() returns a singleton
+    static StackManipulation loadThis() {
+        return LOAD_THIS;
+    }
+
     static StackManipulation invokeMethod(final Method method) {
         return MethodInvocation.invoke(describe(method));
     }
@@ -59,10 +64,6 @@ final class ByteBuddyUtils {
         return new Mark(label);
     }
 
-    static StackManipulation loadThis() {
-        return THIS;
-    }
-
     static StackManipulation getField(final Field field) {
         return FieldAccess.forField(new ForLoadedField(field).asDefined()).read();
     }
index 747af366b4145674fed891342f9f3295afcdcabd..ef0f8a985ff2af1ae0793c63a366d7370c080a32 100644 (file)
@@ -10,9 +10,10 @@ package org.opendaylight.mdsal.binding.dom.codec.impl;
 import static com.google.common.base.Verify.verify;
 import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
-import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.THIS;
+import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.computeFrames;
 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.getField;
 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.invokeMethod;
+import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.loadThis;
 import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.putField;
 
 import com.google.common.collect.ImmutableMap;
@@ -276,7 +277,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         final Generic bindingDef = TypeDefinition.Sort.describe(bindingInterface);
         @SuppressWarnings("unchecked")
         Builder<T> builder = (Builder<T>) BB.subclass(Generic.Builder.parameterizedType(superClass, bindingDef).build())
-            .visit(ByteBuddyUtils.computeFrames()).name(fqcn).implement(bindingDef);
+            .visit(computeFrames()).name(fqcn).implement(bindingDef);
 
         builder = generateGetters(builder);
 
@@ -308,7 +309,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     private static Implementation codecHashCode(final Class<?> bindingInterface) {
         return new Implementation.Simple(
             // return Foo.bindingHashCode(this);
-            THIS,
+            loadThis(),
             invokeMethod(bindingInterface, BindingMapping.BINDING_HASHCODE_NAME, bindingInterface),
             MethodReturn.INTEGER);
     }
@@ -316,7 +317,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     private static Implementation codecEquals(final Class<?> bindingInterface) {
         return new Implementation.Simple(
             // return Foo.bindingEquals(this, obj);
-            THIS,
+            loadThis(),
             FIRST_ARG_REF,
             invokeMethod(bindingInterface, BindingMapping.BINDING_EQUALS_NAME, bindingInterface, Object.class),
             MethodReturn.INTEGER);
@@ -325,7 +326,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
     private static Implementation toString(final Class<?> bindingInterface) {
         return new Implementation.Simple(
             // return Foo.bindingToString(this);
-            THIS,
+            loadThis(),
             invokeMethod(bindingInterface, BindingMapping.BINDING_TO_STRING_NAME, bindingInterface),
             MethodReturn.REFERENCE);
     }
@@ -386,7 +387,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         public ByteCodeAppender appender(final Target implementationTarget) {
             return new ByteCodeAppender.Simple(
                 // return (FooType) codecKey(getFoo$$$V);
-                THIS,
+                loadThis(),
                 getField(implementationTarget.getInstrumentedType(), handleName),
                 CODEC_KEY,
                 TypeCasting.to(retType),
@@ -434,7 +435,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             final TypeDescription instrumentedType = implementationTarget.getInstrumentedType();
             return new ByteCodeAppender.Simple(
                 // return (FooType) codecMember(getFoo$$$V, getFoo$$$S);
-                THIS,
+                loadThis(),
                 getField(instrumentedType, handleName),
                 getField(instrumentedType, stringName),
                 CODEC_MEMBER,
@@ -459,7 +460,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
         public ByteCodeAppender appender(final Target implementationTarget) {
             return new ByteCodeAppender.Simple(
                 // return (FooType) codecMember(getFoo$$$V, FooType.class);
-                THIS,
+                loadThis(),
                 getField(implementationTarget.getInstrumentedType(), handleName),
                 ClassConstant.of(TypeDefinition.Sort.describe(bindingClass).asErasure()),
                 CODEC_MEMBER,
@@ -501,7 +502,7 @@ abstract class CodecDataObjectGenerator<T extends CodecDataObject<?>> implements
             final TypeDescription instrumentedType = implementationTarget.getInstrumentedType();
             return new ByteCodeAppender.Simple(
                 // return (FooType) codecMember(getFoo$$$V, getFoo$$$C);
-                THIS,
+                loadThis(),
                 getField(instrumentedType, handleName),
                 getField(instrumentedType, contextName),
                 CODEC_MEMBER,
index c51374a4a29beb02131509d38e0226bd9eda6646..111b8254f7a3721b1944c532766d2b3897c7609d 100644 (file)
@@ -45,6 +45,7 @@ import net.bytebuddy.implementation.bytecode.member.MethodReturn;
 import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
 import net.bytebuddy.jar.asm.MethodVisitor;
 import net.bytebuddy.jar.asm.Opcodes;
+import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatchers;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingStreamEventWriter;
@@ -453,6 +454,10 @@ final class DataObjectStreamerGenerator<T extends DataObjectStreamer<?>> impleme
     private enum InitializeInstanceField implements ByteCodeAppender {
         INSTANCE;
 
+        // TODO: eliminate this constant when ElementMatchers.isDefaultConstructor() returns a singleton
+        private static final ElementMatcher<MethodDescription> IS_DEFAULT_CONSTRUCTOR =
+            ElementMatchers.isDefaultConstructor();
+
         @Override
         public Size apply(final MethodVisitor methodVisitor, final Context implementationContext,
                 final MethodDescription instrumentedMethod) {
@@ -461,7 +466,7 @@ final class DataObjectStreamerGenerator<T extends DataObjectStreamer<?>> impleme
                 TypeCreation.of(instrumentedType),
                 Duplication.SINGLE,
                 MethodInvocation.invoke(instrumentedType.getDeclaredMethods()
-                    .filter(ElementMatchers.isDefaultConstructor()).getOnly().asDefined()),
+                    .filter(IS_DEFAULT_CONSTRUCTOR).getOnly().asDefined()),
                 putField(instrumentedType, INSTANCE_FIELD))
                     .apply(methodVisitor, implementationContext);
             return new Size(operandStackSize.getMaximalSize(), instrumentedMethod.getStackSize());
index 3388a9a2f0a0e5f7dfaa81ea2ceaadb9a7488f83..a6fe0b5887582c853441f615db33703151990b16 100644 (file)
@@ -8,6 +8,9 @@
 package org.opendaylight.mdsal.binding.dom.codec.impl;
 
 import static com.google.common.base.Preconditions.checkState;
+import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.getField;
+import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.loadThis;
+import static org.opendaylight.mdsal.binding.dom.codec.impl.ByteBuddyUtils.putField;
 
 import com.google.common.base.Throwables;
 import java.lang.invoke.MethodHandle;
@@ -120,7 +123,7 @@ final class NotificationCodecContext<D extends DataObject & Notification>
     private enum ConstructorImplementation implements Implementation {
         INSTANCE;
 
-        private static final StackManipulation INSTANT_ARG = MethodVariableAccess.REFERENCE.loadFrom(3);
+        private static final StackManipulation LOAD_INSTANT_ARG = MethodVariableAccess.REFERENCE.loadFrom(3);
         private static final StackManipulation LOAD_CTOR_ARGS;
 
         static {
@@ -145,12 +148,12 @@ final class NotificationCodecContext<D extends DataObject & Notification>
                     .filter(ElementMatchers.isConstructor()).getOnly();
 
             return new ByteCodeAppender.Simple(
-                ByteBuddyUtils.THIS,
+                loadThis(),
                 LOAD_CTOR_ARGS,
                 MethodInvocation.invoke(superCtor),
-                ByteBuddyUtils.THIS,
-                INSTANT_ARG,
-                ByteBuddyUtils.putField(instrumentedType, INSTANT_FIELD),
+                loadThis(),
+                LOAD_INSTANT_ARG,
+                putField(instrumentedType, INSTANT_FIELD),
                 MethodReturn.VOID);
         }
     }
@@ -166,8 +169,8 @@ final class NotificationCodecContext<D extends DataObject & Notification>
         @Override
         public ByteCodeAppender appender(final Target implementationTarget) {
             return new ByteCodeAppender.Simple(
-              ByteBuddyUtils.THIS,
-              ByteBuddyUtils.getField(implementationTarget.getInstrumentedType(), INSTANT_FIELD),
+              loadThis(),
+              getField(implementationTarget.getInstrumentedType(), INSTANT_FIELD),
               MethodReturn.REFERENCE);
         }
     }