title: How can I bind constructor parameter to a instance field?
body: <p>Is it possible create new final field on a class and create a constructor with parameter that is set to that final field on class instantiation? I tried several approaches and searched for answers but found no example how to do it.</p>
  
  <p>What I have now:</p>
  
  <pre><code>Class proxyClass = new ByteBuddy().subclass(Object.class).implement((Type[]) interfaces)
                          .defineField("dispatcherInvocationHandler", ByteBuddyDispatcherInvocationHandler.class, Modifier.PRIVATE + Modifier.FINAL)
                          .defineConstructor(Modifier.PUBLIC)
                          .withParameter(ByteBuddyDispatcherInvocationHandler.class)
                          .intercept(MethodCall.invokeSuper().andThen(/* copy argument to field */))
                          .method(ElementMatchers.any())
                          .intercept(InvocationHandlerAdapter.toField("dispatcherInvocationHandler"))
                          .make()
                          .load(BytebuddyProxyGenerator.class.getClassLoader())
                          .getLoaded();
  </code></pre>
  
  <p>I don't want to make field public and I don't want to create property with setter / getter to that field. I'd like to end up with something like:</p>
  
  <pre><code>public class DontCare {
      private final ByteBuddyDispatcherInvocationHandler dispatcherInvocationHandler;
  
      public DontCare(ByteBuddyDispatcherInvocationHandler arg) {
         super();
         this.dispatcherInvocationHandler = arg;
      }
  }
  </code></pre>
  
  <p>Is there any simple way how to do this?</p>
  
  <p><strong>Updated 31.10.2016</strong></p>
  
  <p>Finally I got it working with the help of Rafael and 1.5.2 version of the ByteBuddy library. Working code is this:</p>
  
  <pre><code>Class proxyClass = new ByteBuddy().subclass(Object.class).implement((Type[]) interfaces)
                          .defineField("dispatcherInvocationHandler", ByteBuddyDispatcherInvocationHandler.class, Modifier.PRIVATE + Modifier.FINAL)
                          .defineConstructor(Modifier.PUBLIC)
                          .withParameter(ByteBuddyDispatcherInvocationHandler.class)
                          .intercept(
                                  MethodCall.invoke(Object.class.getConstructor())
                                          .onSuper()
                                          .andThen(
                                                  FieldAccessor.ofField("dispatcherInvocationHandler")
                                                          .setsArgumentAt(0)
                                          )
                          )
                          .method(ElementMatchers.any())
                          .intercept(InvocationHandlerAdapter.toField("dispatcherInvocationHandler"))
                          .make()
                          .load(ByteBuddyProxyGenerator.class.getClassLoader())
                          .getLoaded();
  </code></pre>