byte-buddy

How to transfer data to/from method argument to self in Bytebuddy?


I have a base class

public abstract class Cache {
    public abstract void load(Object input);
    public abstract void dump(Object output);
}

Would like to subclass it in Bytebuddy and

  1. define a certain number of fields (This I am able to do)
  2. implement to 2 abstract methods:

Tried FieldAccessor but seems the API isn't suited for this purpose? Couldn't find any solution online except for this ancient answer which is supposedly addressed but I can't find the corresponding code How to get the value from each field of a class with Byte Buddy?


Example

public class Input {
   double foo;
   String bar;
}
public class Output {
   boolean z;
}

Based on this supplied classes (Input & Output), extend/implement a Cache class with bytebuddy such that it does the following

public class ByteBuddyCache extends Cache {

    double foo;
    String bar;
    boolean z;

    public void load(Object input) {
        Input i = (Input) input;
        this.foo = i.foo;
        this.bar = i.bar;
    }
    public void dump(Object output) {
        Output o = (Output) output;
        o.z = this.z;
    }
}

Solution

  • This would require you to create custom StackManipulations. In these, you would combine FieldAccess objects and MethodVariableAccess.