How to create a Byte Buddy proxy with MethodDelegation or Forwarding?
<p>I get an exception in the load call when I try to do the following:</p>

<pre><code>  Field datasourceExtensionField = Grid.class.getDeclaredField("datasourceExtension");
  datasourceExtensionField.setAccessible(true);
  RpcDataProviderExtension rpcDataProviderExtension = (RpcDataProviderExtension) datasourceExtensionField.get(grid);

  Field activeItemHandlerField = RpcDataProviderExtension.class.getDeclaredField("activeItemHandler");
  activeItemHandlerField.setAccessible(true);
  Object activeItemHandler = activeItemHandlerField.get(rpcDataProviderExtension);

  Field keyMapperField = activeItemHandler.getClass().getDeclaredField("keyMapper");
  keyMapperField.setAccessible(true);
  KeyMapper original = (KeyMapper) keyMapperField.get(activeItemHandler);

  KeyMapper wrapper = new ByteBuddy() //
      .subclass(KeyMapper.class) //
      .defineField("original", KeyMapper.class, Visibility.PUBLIC) //
      .method(ElementMatchers.any()) //
      .intercept(Forwarding.toField("original")) //
      .method(ElementMatchers.named("get")) //
      .intercept(MethodDelegation.to(new KeyMapperWrapper(grid, original))) //
      .make() //
      .load(KeyMapperWrapper.class.getClassLoader()) //
      .getLoaded() //
      .newInstance();

  // give wrapper the reference to the original
  wrapper.getClass().getDeclaredField("original").set(wrapper, original);

  // replace original with wrapper
  keyMapperField.set(activeItemHandler, wrapper);
</code></pre>

<p>The exception:</p>

<pre><code>java.lang.VerifyError: Bad access to protected data in invokevirtual
Exception Details:
  Location:
    com/vaadin/server/KeyMapper$ByteBuddy$WlWljaQa.clone()Ljava/lang/Object; @4: invokevirtual
  Reason:
    Type 'com/vaadin/server/KeyMapper' (current frame, stack[0]) is not assignable to 'com/vaadin/server/KeyMapper$ByteBuddy$WlWljaQa'
  Current Frame:
    bci: @4
    flags: { }
    locals: { 'com/vaadin/server/KeyMapper$ByteBuddy$WlWljaQa' }
    stack: { 'com/vaadin/server/KeyMapper' }
  Bytecode:
    0x0000000: 2ab4 000c b600 1cb0                    

    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
    at java.lang.Class.getDeclaredField(Class.java:2068)
    at net.bytebuddy.implementation.LoadedTypeInitializer$ForStaticField.onLoad(LoadedTypeInitializer.java:101)
    at net.bytebuddy.implementation.LoadedTypeInitializer$Compound.onLoad(LoadedTypeInitializer.java:180)
    at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:75)
    at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4525)
    at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4514)
    at test.KeyMapperWrapper.patch(KeyMapperWrapper.java:62)
</code></pre>

<p>I'm apparently not understanding how <code>Forwarding</code> is supposed to work, what am I doing wrong?</p>

<p>My intention is to replace the existing <code>KeyMapper</code> with a proxy where I <em>override</em> one single method, and delegate the rest to the original.</p>

<p>Edit: I now also tried with <code>MethodDelegation</code>, which throws the same exception:</p>

<pre><code> .method(ElementMatchers.any()) //
 .intercept(MethodDelegation.to(original)) //
 .method(ElementMatchers.named("get")) //
 .intercept(MethodDelegation.to(new KeyMapperWrapper(grid, original))) //
</code></pre>