Attributes carry what a class file holds beyond its outline: each method’s bytecode, constant values, declared exceptions and debugging information. The class, each field, each method and each Code attribute end with a list of them. Every attribute names itself and gives its length, so a reader can skip any it does not know.
attribute_info
Each list is a u2 attributes_count followed by that many attributes,
each of this shape:
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 0 | 2 | u2 | attribute_name_index | A Utf8 entry: the attribute’s name |
| 2 | 4 | u4 | attribute_length | The number of bytes that follow, not counting these six |
| 6 | attribute_length | u1[] | info | The body, whose layout the name decides |
The explorer reads each of the format’s attributes field by field, inside a window of
exactly attribute_length bytes, so a body longer or shorter than its
fields is found at once.
The attributes of this format
| Attribute | Where | Purpose |
|---|---|---|
| ConstantValue | field | A constant field’s value |
| Code | method | The bytecode, with this format’s 4-byte header |
| Exceptions | method | The checked exceptions the method declares |
| SourceFile | class | The source file’s name |
| LineNumberTable | Code | Source line numbers |
| LocalVariableTable | Code | Local variable names and types |
These are the six attributes JDK 1.0.2 knew. Whether the pre-release compilers
wrote all of them is not known. Offsets below count from the start of the attribute,
so 6 is the first byte after attribute_length.
ConstantValue
On a field: the constant value a static field starts with. The length is 2.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 6 | 2 | u2 | constantvalue_index | An Integer entry for int, short, char, byte and boolean fields; Float, Long or Double for those types; String for a String field |
Exceptions
On a method: the checked exceptions in its throws clause.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 6 | 2 | u2 | number_of_exceptions | The number of entries |
| 8 | 2 × n | u2[] | exception_index_table | Class entries, one per exception class |
SourceFile
On a class: the name of the file it was compiled from, without any folder. The length is 2.
| Offset | Size | Type | Name | Meaning |
|---|---|---|---|---|
| 6 | 2 | u2 | sourcefile_index | A Utf8 entry: the file name |
In the example file,
00 0C 00 00 00 02 00 09 is a SourceFile attribute naming
EmptyStackException.java.
Other attributes
A reader skips an attribute it does not know, using its length. The explorer shows any attribute other than the six above as plain bytes, “not a standard attribute”. The names of attributes from later class file formats are known to it, but in a file of this format they are shown as bytes too, as not belonging to this layout. They include:
| Attribute | Later meaning |
|---|---|
| InnerClasses, Synthetic, Deprecated | Nested classes, compiler-made members and deprecation, from JDK 1.1 |
| Signature, EnclosingMethod, the annotation attributes | Generics and annotations, from J2SE 5.0 |
| StackMapTable | Type frames for the verifier, from Java SE 6 |
| BootstrapMethods | Bootstrap methods for dynamic call sites, from Java SE 7 |
| Module, NestHost, NestMembers, Record, PermittedSubclasses | Modules, nests, records and sealed classes, from Java SE 9 to 17 |