StackMapTable attribute
The types in each local variable and on the operand stack at every branch target and handler.
- Appears on
- Code
- Since
- class file 50.0 (Java 6)
- javac writes it
- for every method with a branch or an exception handler (class files 50.0 and later)
- Needed by the JVM
- yes: the JVM reads it to load, verify or run the class
Layout
| Type | Field | What it holds |
|---|---|---|
u2 | attribute_name_index | a Utf8: "StackMapTable" |
u4 | attribute_length | bytes after this field |
u2 | number_of_entries | |
stack_map_frame[…] | entries | one compressed frame per branch target, in pc order |
What it is for
The types in each local variable and on the operand stack at every branch target and handler. With it the verifier checks a method in one pass, instead of inferring the types by iterating to a fixed point, which made class loading slower and verification complicated.
Frames are compressed: each one is stored as a delta from the previous frame (same, same_locals_1_stack_item, chop, append, full_frame), and the pc as an offset from the previous frame's pc.
It became mandatory with class file version 51 (Java 7). It is also why bytecode generators have to compute frames, which ASM's COMPUTE_FRAMES does.
How common it is
JDK 25.0.5's java.base has 23,998 StackMapTable attributes, in 4,132 of its 7,391 classes (23,998 on Code attributes).
A real one, byte by byte
From Hello.class (compiled by javac 25), on method main, in its Code attribute:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1024 | 13 bytes | StackMapTable | |
| 1024 | 00 26 | attribute_name_index | #38 |
| 1026 | 00 00 00 07 | attribute_length | 7 |
| 1030 | 00 02 | number_of_entries | 2 |
| 1032 | 1 bytes | frame 0 | @11 same |
| 1032 | 0b | frame_type | 11 |
| 1033 | 4 bytes | frame 1 | @13 same_locals_1_stack_item |
| 1033 | 41 | frame_type | 65 |
| 1034 | 3 bytes | stack | |
| 1034 | 07 | tag | Object |
| 1035 | 00 27 | cpool_index | #39 |
Specification: JVMS §4.7, The StackMapTable Attribute (Java SE 25).