Search code examples
javajava-bytecode-asmbyte-buddy

Is it possible to access local variables with ByteBuddy's Advice?


When intercepting a method's implementation with an @Advice, is it possible to access local variables?


Solution

  • No, it is not and this is partly by design. A method's implementation is supposed to be a private detail whereas advice adds code after and before an invocation where the method's signature is part of its API.

    At some point, Byte Buddy might offer a possibility to replace certain field and method accesses but in a way that you do not rely on an implementation.

    In order to mingle with the explicit byte code, Byte Buddy offers AsmVisitorWrapper to use the ASM API directly which is better suited for byte code-level manipulation than Byte Buddy's high-level API.