Suppose I have a method m
:
public void m() {
String foo = "foo";
int bar = 0;
doSomething(foo, bar);
}
I want to use ByteBuddy to instrument the code so that when calling doSomething
in m
, it will automatically put the value of foo
and bar
into a HashMap
, pretty much something looks like:
public void m() {
String foo = "foo";
int bar = 0;
context.put("foo", foo); // new code injected
context.put("bar", bar); // new code injected
doSomething(foo, bar);
}
Is there anyway to do this instrumentation via ByteBuddy?
There is built-in way in Byte Buddy to do redefine method m
in this way. Byte Buddy is however voluntarily exposing the ASM API on top of which Byte Buddy is implemented. ASM offers quite extensive documentation which would show you how to do this. I can however tell you that it will be quite a lot of code. Note that you require to compile any method with debug symbols enabled, otherwise these internal variables are not available at run time.
Are you however sure you want to do this? Without knowing your exact use case, it feels like it is a bad idea. By implementing this solution, you make the names of local variables a part of your application instead of letting them be an implementation detail.
I would therefore suggest you to rather instrument the doSomething
method. Would this suffice yourn what is easily done in Byte Buddy using an interceptor like the following:
class Interceptor {
void intercept(@Origin Method method, @AllArguments Object[] args) {
int index = 0;
for(Parameter p : method.getParameters()) {
context.add(p.getName(), args[index++]);
}
}
}
This interceptor could then be used as follows:
MethodDelegation.to(new Interceptor()).andThen(SuperMethodCall.INSTANCE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With