Module java.base
Package java.lang.invoke

Class MethodType

java.lang.Object
java.lang.invoke.MethodType
All Implemented Interfaces:
Serializable,Constable,TypeDescriptor,TypeDescriptor.OfMethod<Class<?>,MethodType>

public final classMethodTypeextendsObjectimplementsConstable,TypeDescriptor.OfMethod<Class<?>,MethodType>,Serializable
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 toMethodHandle.invokeExact andMethodHandle.invoke, and during execution ofinvokedynamic instructions.

The structure is a return type accompanied by any number of parameter types. The types (primitive,void, and reference) are represented byClass objects. (For ease of exposition, we treatvoid as if it were a type. In fact, it denotes the absence of a return type.)

All instances ofMethodType 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 methodsparameterArray andparameterList also provide a choice between arrays and lists.

MethodType objects are sometimes derived from bytecode instructions such asinvokedynamic, 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 anldc instruction which refers to a suitableCONSTANT_MethodType constant pool entry. The entry refers to aCONSTANT_Utf8 spelling for the descriptor string. (For full details on method type constants, see sections4.4.8 and5.4.3.5 of the Java Virtual Machine Specification.)

When the JVM materializes aMethodType 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 aCONSTANT_Class.) This loading may occur at any time before theMethodType object is first derived.

Nominal Descriptors

AMethodType can be described innominal form if and only if all of the parameter types and return type can be described with anominal descriptor represented byClassDesc. If a method type can be described nominally, then:

If any of the parameter types or return type cannot be described nominally, i.e.Class::describeConstable returns an empty optional for that type, then the method type cannot be described nominally:

Since:
1.7
See Also: