Search code examples
javabytecode-manipulationbyte-buddyjvm-bytecode

How to count local variable indexes in MethodVariableAccess?


According to [1], in a method frame, the local variable array contains a reference to the called instance, the parameters and, finally, any other variables used in the method's code. Also, long and double values occupies two local variables.

When using Byte Buddy to generate the method's code via stack manipulations, does the MethodVariableAccess.OffsetLoading index long and double values into a single index or is it needed, as using ASM directly, to account for these kinds of values to calculate a local var index?

[1] https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6.1


Solution

  • MethodVariableAccess.OffsetLoading accesses an offset which is not named index to distinguish exactly that. If a long or double type is contained in the array, this accounts to two slots. Byte Buddy uses this abstraction to interact with ASM where the same offset is required for calling the respective visitor.