A method type represents the arguments and return type accepted and returned by a method handle, or the arguments and return type passed and expected by a method handle caller. Method types must be properly matched between a method handle and all its callers, and the JVM's operations enforce this matching at, specifically during calls to {@link MethodHandle#invokeExact MethodHandle.invokeExact}and {@link MethodHandle#invoke MethodHandle.invoke}, and during execution of {@code invokedynamic} instructions.
The structure is a return type accompanied by any number of parameter types. The types (primitive, {@code void}, and reference) are represented by {@link Class} objects.(For ease of exposition, we treat {@code void} as if it were a type.In fact, it denotes the absence of a return type.)
All instances of {@code MethodType} are immutable.Two instances are completely interchangeable if they compare equal. Equality depends on pairwise correspondence of the return and parameter types and on nothing else.
This type can be created only by factory methods. All factory methods may cache values, though caching is not guaranteed. Some factory methods are static, while others are virtual methods which modify precursor method types, e.g., by changing a selected parameter.
Factory methods which operate on groups of parameter types are systematically presented in two versions, so that both Java arrays and Java lists can be used to work with groups of parameter types. The query methods {@code parameterArray} and {@code parameterList}also provide a choice between arrays and lists.
{@code MethodType} objects are sometimes derived from bytecode instructionssuch as {@code invokedynamic}, specifically from the type descriptor strings associated with the instructions in a class file's constant pool.
Like classes and strings, method types can also be represented directly in a class file's constant pool as constants. A method type may be loaded by an {@code ldc} instruction which refersto a suitable {@code CONSTANT_MethodType} constant pool entry.The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string.(For full details on method type constants, see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.)
When the JVM materializes a {@code MethodType} from a descriptor string,all classes named in the descriptor must be accessible, and will be loaded. (But the classes need not be initialized, as is the case with a {@code CONSTANT_Class}.) This loading may occur at any time before the {@code MethodType} object is first derived.
@author John Rose, JSR 292 EG