0

I'm using ByteBuddy and I'm trying to implement the equivalent of:

void foo(A a, B b) {
     b.method(a.field)
}

I can do:

void foo(A a, B b) {
     b.method(a)
}

by code like:

 java.lang.reflect.Method method = B.class.getMethod("method", A.class);
 MethodCall
            .invoke(method)
            .onArgument(1)
            .withArgument(0);

but I can't work out how to do:

void foo(A a, B b) {
     b.method(a.field)
}

Using "withField" appears to be the equivalent of "this.field", whereas I want "a.field". Is there a standard way to do this, or am I going to have to write my own ArgumentLoader implementation to do this? In which case, what might that consist of?

I might be able to get away with "getter" access, rather than "direct" access, but actually I can't figure out how to do that either!

Thanks.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Tom Quarendon
  • 5,625
  • 5
  • 23
  • 30
  • I've done it by down "down" a level and creating some custom StackManipulations. Doesn't *appear* to be a way to do it more directly. – Tom Quarendon Mar 17 '17 at 16:56

1 Answers1

0

There is not currently a good way but I am working on a way to extend the component for such use. Currently (version 1.6.11), the only way of doing what you want is by using custom StackManipulations as you suggest in your comments.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • To be honest I've found that ByteBuddy is good for the use cases that it seems to be intended for, but I've struggled to do other things, like just construct general purpose methods. Particularly I've failed to create a local variable so far. – Tom Quarendon Mar 20 '17 at 08:25
  • The idea of Byte Buddy is that the generated byte code layer is as thin as possible. You should rather use delegation and advice for your purposes. If you want to generate complex code, ASM offers a good API for such manipulation. – Rafael Winterhalter Mar 20 '17 at 09:11