MethodParameters attribute
Parameter names and flags, for reflection: Parameter.getName() returns the real name only when this attribute is present, and arg0, arg1 otherwise.
- Appears on
- method
- Since
- class file 52.0 (Java 8)
- javac writes it
- with -parameters, and for some compiler-generated parameters
- Needed by the JVM
- no: it is for the class libraries (reflection), compilers or tools
Layout
| Type | Field | What it holds |
|---|---|---|
u2 | attribute_name_index | a Utf8: "MethodParameters" |
u4 | attribute_length | bytes after this field |
u1 | parameters_count | |
{u2,u2}[…] | parameters | name_index, access_flags (final, synthetic, mandated) |
What it is for
Parameter names and flags, for reflection: Parameter.getName() returns the real name only when this attribute is present, and arg0, arg1 otherwise. Frameworks that bind by parameter name (Spring, Jackson) rely on javac's -parameters flag.
javac writes it without being asked when a method has parameters that the source does not show or that the language requires: the outer instance of an inner class's constructor, an enum's valueOf, bridge methods and a record's canonical constructor.
How common it is
JDK 25.0.5's java.base has 2,977 MethodParameters attributes, in 1,566 of its 7,391 classes (2,977 on methods).
A real one, byte by byte
From Bridge.class (compiled by javac 25), on method compareTo:
| Offset | Bytes | Field | Value |
|---|---|---|---|
| 1191 | 11 bytes | MethodParameters | |
| 1191 | 00 37 | attribute_name_index | #55 |
| 1193 | 00 00 00 05 | attribute_length | 5 |
| 1197 | 01 | parameters_count | 1 |
| 1198 | 4 bytes | parameter 0 | |
| 1198 | 00 00 | name_index | #0 |
| 1200 | 10 00 | access_flags | 0x1000 synthetic |
Specification: JVMS §4.7, The MethodParameters Attribute (Java SE 25).