Byte Buddy has a built-in way to override the m method this way. Byte Buddy, however, voluntarily discloses the ASM API, on top of which Byte Buddy is implemented. ASM offers quite extensive documentation that will show you how to do this. However, I can say that it will be quite a lot of code. Note that you need to compile any method with debugging symbols turned on, otherwise these internal variables are not available at runtime.
Are you sure you want to do this? Not knowing your specific use case, it seems like a bad idea. By implementing this solution, you make local variable names part of your application, rather than letting them be implementation details.
Therefore, I suggest you rather use the doSomething method. Will it be enough, which is easy to do in Byte Buddy using an interceptor as shown below:
class Interceptor { void intercept(@Origin Method method, @AllArguments Object[] args) { int index = 0; for(Parameter p : method.getParameters()) { context.add(p.getName(), args[index++]); } } }
Then this interceptor can be used as follows:
MethodDelegation.to(new Interceptor()).andThen(SuperMethodCall.INSTANCE);
source share