简体   繁体   中英

ByteBuddy: Pass field of object to method

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.

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 StackManipulation s as you suggest in your comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM