LocalVariableIndices - crowlogic/arb4j GitHub Wiki

In the context of java bytecode, both method parameters and variables defined in the body of the method are treated as local variables. They are stored in an array-like data structure called the local variable table. Each local variable in the method has an index in this table.

The first few entries of the local variable table are taken up by the method's parameters, starting from index 0. For instance methods, the zero index is occupied by this. After the parameters, local variables defined in the method body start filling up the subsequent entries in the table.

The visitLocalVariable method of the MethodVisitor class in the ASM library is used to generate entries in the LocalVariableTable attribute of a method in the bytecode. This is typically used to associate local variable indices with names and types, which can be useful for debugging or decompiling. Note that the LocalVariableTable attribute is optional and is not used by the JVM at runtime.

Here's an example:

public void myMethod(int param1, float param2) {
    double local1 = 123.45;
    String local2 = "hello";
    // ...
}

In bytecode, the local variable table for this method might look something like this:

0: this
1: param1
2: param2
3: local1
4: local2