Bind invoker method handle to constructor 31/78131/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Nov 2018 15:15:23 +0000 (16:15 +0100)
committerRobert Varga <nite@hq.sk>
Mon, 26 Nov 2018 10:06:02 +0000 (10:06 +0000)
We only ever pass the constructor method handle to the invoker,
hence we can make this a constant by binding the invoker handle
to the constructor -- saving us a field.

Change-Id: I547f7eac202389aa0f7308266b2f3a451a2f83a3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 115d5e187372ec39779e97d0ab6c5cc8140a94e9)

binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/IdentifiableItemCodec.java

index 8e06d5ae548cb5d98d513a7900cc3734a9390889..ad090fb1bf078596a267c522ee27444664c315e4 100644 (file)
@@ -32,7 +32,6 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
     private final List<QName> keysInBindingOrder;
     private final ListSchemaNode schema;
     private final Class<?> identifiable;
-    private final MethodHandle ctorInvoker;
     private final MethodHandle ctor;
 
     IdentifiableItemCodec(final ListSchemaNode schema, final Class<? extends Identifier<?>> keyClass,
@@ -40,13 +39,14 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
         this.schema = schema;
         this.identifiable = identifiable;
 
+        final MethodHandle tmpCtor;
         try {
-            ctor = MethodHandles.publicLookup().unreflectConstructor(getConstructor(keyClass));
+            tmpCtor = MethodHandles.publicLookup().unreflectConstructor(getConstructor(keyClass));
         } catch (IllegalAccessException e) {
             throw new IllegalArgumentException("Missing constructor in class " + keyClass, e);
         }
-        final MethodHandle inv = MethodHandles.spreadInvoker(ctor.type(), 0);
-        this.ctorInvoker = inv.asType(inv.type().changeReturnType(Identifier.class));
+        final MethodHandle inv = MethodHandles.spreadInvoker(tmpCtor.type(), 0);
+        this.ctor = inv.asType(inv.type().changeReturnType(Identifier.class)).bindTo(tmpCtor);
 
         /*
          * We need to re-index to make sure we instantiate nodes in the order in which
@@ -99,7 +99,7 @@ final class IdentifiableItemCodec implements Codec<NodeIdentifierWithPredicates,
 
         final Identifier<?> identifier;
         try {
-            identifier = (Identifier<?>) ctorInvoker.invokeExact(ctor, bindingValues);
+            identifier = (Identifier<?>) ctor.invokeExact(bindingValues);
         } catch (Throwable e) {
             Throwables.throwIfUnchecked(e);
             throw new RuntimeException(e);