简体   繁体   中英

What do 'start' and 'length' attribute in LocalVariableTable mean

So here is the example:

LocalVariableTable:
        Start  Length  Slot  Name   Signature
               0     133     0  this   Lcom/my/class/Test;
               2     131     1     a   I
               4     129     2     b   I
               7     126     3     i   I
              10     123     4    i2   I
              16     117     5    o1   Ljava/lang/Integer;
              31     102     6    o2   Ljava/lang/Integer;

What does start and length mean? Why does length have the value it has? Why length is different for the equal types (Integer)? Why length could be changed, when I add something to class and recompile it without touching that particular local variable?

Start is the start bytecode offset where this variable is visible. Length is number of bytecode bytes during which this variable is visible. Usually start points to the bytecode instruction where the variable is first assigned or to 0 for method parameters and this . In your case it seems that all variables are valid to the end of the method ( start+length = 133 for every variable), but if you declare some variables inside blocks, their scope will be shorter.

Note that local variables table (LVT) is an optional debugging information. It's not necessary for program execution and can be switched off using -g:none during the compilation. The main purpose of this table is to make debugging more convenient: having it you can determine for each bytecode positions which variables are currently visible to display them in variables pane and hide them once you step out of the variable scope. Also this table is used by java decompilers and code analyzers like FindBugs.

According to jsl

Inside Local variabale Table

u2 local_variable_table_length;
    {   u2 start_pc;
        u2 length;
        u2 name_index;
        u2 descriptor_index;
        u2 index;
    }

Each entry in the local_variable_table array indicates a range of code array offsets within which a local variable has a value. It also indicates the index into the local variable array of the current frame at which that local variable can be found.

Now for your start and length attribute JSL says that

start_pc, length

The given local variable must have a value at indices into the code 

array in the interval [start_pc, start_pc + length), that is, between start_pc inclusive and start_pc + length exclusive.

The value of start_pc must be a valid index into the code array of this 

Code attribute and must be the index of the opcode of an instruction.

The value of start_pc + length must either be a valid index into 

the code array of this Code attribute and be the index of the opcode of an instruction, or it must be the first index beyond the end of that code array.

So basically starts corresponds to your LineNumberTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM