APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.lang.invoke

public final Class MethodType

extends Object
implements Constable, TypeDescriptor.OfMethod<Class<?>, MethodType>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.invoke.TypeDescriptor.OfMethod, java.lang.invoke.TypeDescriptor, java.lang.constant.Constable
Imports
java.lang.constant.ClassDesc, .Constable, .MethodTypeDesc, java.util.Arrays, .Collections, .List, .Map, .Objects, .Optional, .StringJoiner, java.util.function.Supplier, java.util.concurrent.ConcurrentHashMap, jdk.internal.util.ReferencedKeySet, .ReferenceKey, jdk.internal.vm.annotation.Stable, sun.invoke.util.BytecodeDescriptor, .VerifyType, .Wrapper

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 MethodHandle.invokeExact and MethodHandle.invoke, and during execution of invokedynamic instructions.

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

All instances of 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 parameterArray and parameterList also provide a choice between arrays and lists.

MethodType objects are sometimes derived from bytecode instructions such as 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 ldc instruction which refers to a suitable CONSTANT_MethodType constant pool entry. The entry refers to a 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 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 CONSTANT_Class.) This loading may occur at any time before the MethodType object is first derived.

Nominal Descriptors

A MethodType can be described in nominal form if and only if all of the parameter types and return type can be described with a nominal descriptor represented by ClassDesc. 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:

Author
John Rose, JSR 292 EG
Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private MethodTypeForm
pack-priv static final ReferencedKeySet<MethodType>
private Invokers
pack-priv static final int
MAX_JVM_ARITY

This number, mandated by the JVM spec as 255, is the maximum number of slots that any Java method can receive in its argument list.

pack-priv static final int
MAX_MH_ARITY

This number is the maximum arity of a method handle, 254.

pack-priv static final int
MAX_MH_INVOKER_ARITY

This number is the maximum arity of a method handle invoker, 253.

private static final Class<?>[]
private String
pack-priv static final Class<?>[]
private static final MethodType[]
private final Class<?>[]
private final Class<?>
private static final ObjectStreamField[]
serialPersistentFields

There are no serializable fields for MethodType.

private static final long
private Object

Constructor Summary

AccessConstructor and Description
private
MethodType(Class<?> rtype, Class<?>[] ptypes)

Constructor that performs no copying or validation.

Method Summary

Modifier and TypeMethod and Description
public MethodType

Returns:

the same type, except with the selected parameter(s) appended
appendParameterTypes
(Class<?>...
zero or more new parameter types to insert after the end of the parameter list
ptypesToInsert
)

Finds or creates a method type with additional parameter types.

public MethodType

Returns:

the same type, except with the selected parameter(s) appended
appendParameterTypes
(List<Class<?>>
zero or more new parameter types to insert after the end of the parameter list
ptypesToInsert
)

Finds or creates a method type with additional parameter types.

pack-priv MethodType

Returns:

the resulting type
asCollectorType
(Class<?>
any array type
arrayType
,
int
position at which to insert parameters
pos
,
int
the number of parameter types to insert
arrayLength
)

Delete the last parameter type and replace it with arrayLength copies of the component type of arrayType.

private static void
pack-priv MethodType

Returns:

the resulting type
asSpreaderType
(Class<?>
any array type
arrayType
,
int
position at which to spread
pos
,
int
the number of parameter types to change
arrayLength
)

Replace the last arrayLength parameter types with the component type of arrayType.

pack-priv MethodType

Returns:

a version of the original type with all reference and subword types replaced
basicType
()

Erases all reference types to Object, and all subword types to int.

pack-priv static boolean
canConvert(Class<?> src, Class<?> dst)

private boolean
canConvertParameters(Class<?>[] srcTypes, Class<?>[] dstTypes)

public MethodType

Returns:

the same type, except with the selected parameter changed
changeParameterType
(int
the index (zero-based) of the parameter type to change
num
,
Class<?>
a new parameter type to replace the old one with
nptype
)

Implements java.lang.invoke.TypeDescriptor.OfMethod.changeParameterType.

Finds or creates a method type with a single different parameter type.

public MethodType

Returns:

the same type, except with the return type change
changeReturnType
(Class<?>
a return parameter type to replace the old one with
nrtype
)

Implements java.lang.invoke.TypeDescriptor.OfMethod.changeReturnType.

Finds or creates a method type with a different return type.

private static int
checkPtypes(Class<?>[] ptypes)

Return number of extra slots (count of long/double args).

pack-priv static void
checkSlotCount(int count)

public Optional<MethodTypeDesc>

Returns:

An Optional containing the resulting nominal descriptor, or an empty Optional if one cannot be constructed.
describeConstable
()

Implements java.lang.constant.Constable.describeConstable.

Returns a nominal descriptor for this instance, if one can be constructed, or an empty Optional if one cannot be.

public String

Returns:

the descriptor string for this method type
descriptorString
()

Implements java.lang.invoke.TypeDescriptor.descriptorString.

Returns the descriptor string for this method type.

public MethodType

Returns:

the same type, except with the selected parameter(s) removed
dropParameterTypes
(int
the index (zero-based) of the first parameter type to remove
start
,
int
the index (greater than start) of the first parameter type after not to remove
end
)

Implements java.lang.invoke.TypeDescriptor.OfMethod.dropParameterTypes.

Finds or creates a method type with some parameter types omitted.

pack-priv boolean
effectivelyIdenticalParameters(int skipPos, List<Class<?>> fullList)

True if my parameter list is effectively identical to the given full list, after skipping the given number of my own initial parameters.

public boolean
equals(Object
object to compare
x
)

Overrides java.lang.Object.equals.

Compares the specified object with this type for equality.

private boolean
public MethodType

Returns:

a version of the original type with all reference types replaced
erase
()

Erases all reference types to Object.

pack-priv boolean
explicitCastEquivalentToAsType(MethodType newType)

Returns true if MHs.explicitCastArguments produces the same result as MH.asType.

private static boolean
explicitCastEquivalentToAsType(Class<?> src, Class<?> dst)

Reports true if the src can be converted to the dst, by both asType and MHs.eCE, and with the same effect.

pack-priv MethodTypeForm
form()

pack-priv static MethodType
fromDescriptor(String descriptor, ClassLoader loader)

Same as fromMethodDescriptorString(String, ClassLoader), but null ClassLoader means the bootstrap loader is used here.

public static MethodType

Returns:

a method type of the given method descriptor
fromMethodDescriptorString
(String
a method descriptor string
descriptor
,
ClassLoader
the class loader in which to look up the types
loader
)

Finds or creates an instance of a method type of the given method descriptor (JVMS 4.3.3).

public MethodType

Returns:

a version of the original type with all types replaced
generic
()

Converts all types, both reference and primitive, to Object.

public static MethodType

Returns:

a generally applicable method type, for all calls of the given fixed argument count and a collected array of further arguments
genericMethodType
(int
number of parameters (excluding the final array parameter if any)
objectArgCount
,
boolean
whether there will be a trailing array parameter, of type Object[]
finalArray
)

Finds or creates a method type whose components are Object with an optional trailing Object[] array.

public static MethodType

Returns:

a generally applicable method type, for all calls of the given argument count
genericMethodType
(int
number of parameters
objectArgCount
)

Finds or creates a method type whose components are all Object.

public int

Returns:

the hash code value for this method type
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the hash code value for this method type.

public boolean

Returns:

true if any of the types are primitives
hasPrimitives
()

Reports if this type contains a primitive argument or return value.

public boolean

Returns:

true if any of the types are wrappers
hasWrappers
()

Reports if this type contains a wrapper argument or return value.

public MethodType

Returns:

the same type, except with the selected parameter(s) inserted
insertParameterTypes
(int
the position (zero-based) of the inserted parameter type(s)
num
,
Class<?>...
zero or more new parameter types to insert into the parameter list
ptypesToInsert
)

Implements java.lang.invoke.TypeDescriptor.OfMethod.insertParameterTypes.

Finds or creates a method type with additional parameter types.

public MethodType

Returns:

the same type, except with the selected parameter(s) inserted
insertParameterTypes
(int
the position (zero-based) of the inserted parameter type(s)
num
,
List<Class<?>>
zero or more new parameter types to insert into the parameter list
ptypesToInsert
)

Finds or creates a method type with additional parameter types.

pack-priv Invokers
pack-priv MethodType
invokerType()

Returns: a version of the original type with MethodHandle prepended as the first argument

private static boolean
isAllObject(Class<?>[] ptypes, int to)

pack-priv boolean
pack-priv boolean
pack-priv boolean
isViewableAs(MethodType newType, boolean keepInterfaces)

True if the old return type can always be viewed (w/o casting) under new return type, and the new parameters can be viewed (w/o casting) under the old parameter types.

public Class<?>

Returns:

the last parameter type if any, else void.class
lastParameterType
()

Returns the last parameter type of this method type.

pack-priv Class<?>

Returns:

the leading parameter type, after error checks
leadingReferenceParameter
()

Return the leading parameter type, which must exist and be a reference.

private static Class<?>[]
listToArray(List<Class<?>> ptypes)

private static MethodType

Returns:

the unique method type of the desired structure
makeImpl
(Class<?>
desired return type
rtype
,
Class<?>[]
desired parameter types
ptypes
,
boolean
whether the ptypes can be used without cloning
trusted
)

Sole factory method to find or create an interned method type.

public static MethodType

Returns:

a method type with the given components
methodType
(Class<?>
the return type
rtype
,
Class<?>[]
the parameter types
ptypes
)

Finds or creates an instance of the given method type.

public static MethodType

Returns:

a method type with the given components
methodType
(Class<?>
the return type
rtype
,
List<Class<?>>
the parameter types
ptypes
)

Finds or creates a method type with the given components.

public static MethodType

Returns:

a method type with the given components
methodType
(Class<?>
the return type
rtype
,
Class<?>
the first parameter type
ptype0
,
Class<?>...
the remaining parameter types
ptypes
)

Finds or creates a method type with the given components.

public static MethodType

Returns:

a method type with the given return value
methodType
(Class<?>
the return type
rtype
)

Finds or creates a method type with the given components.

public static MethodType

Returns:

a method type with the given return value and parameter type
methodType
(Class<?>
the return type
rtype
,
Class<?>
the parameter type
ptype0
)

Finds or creates a method type with the given components.

public static MethodType

Returns:

a method type with the given components
methodType
(Class<?>
the return type
rtype
,
MethodType
the method type which supplies the parameter types
ptypes
)

Finds or creates a method type with the given components.

pack-priv static MethodType
methodType(Class<?> rtype, Class<?>[] ptypes, boolean trusted)

private static IndexOutOfBoundsException
public Class<?>[]

Returns:

the parameter types (as a fresh copy if necessary)
parameterArray
()

Implements java.lang.invoke.TypeDescriptor.OfMethod.parameterArray.

Presents the parameter types as an array (a convenience method).

public int

Returns:

the number of parameter types
parameterCount
()

Implements java.lang.invoke.TypeDescriptor.OfMethod.parameterCount.

Returns the number of parameter types in this method type.

public List<Class<?>>

Returns:

the parameter types (as an immutable list)
parameterList
()

Implements java.lang.invoke.TypeDescriptor.OfMethod.parameterList.

Presents the parameter types as a list (a convenience method).

pack-priv int

Returns:

the number of JVM stack slots for this type's parameters
parameterSlotCount
()

Reports the number of JVM stack slots required to invoke a method of this type.

public Class<?>

Returns:

the selected parameter type
parameterType
(int
the index (zero-based) of the desired parameter type
num
)

Implements java.lang.invoke.TypeDescriptor.OfMethod.parameterType.

Returns the parameter type at the specified index, within this method type.

pack-priv Class<?>[]
private void
readObject(ObjectInputStream
the stream to read the object from
s
)

Reconstitute the MethodType instance from a stream (that is, deserialize it).

private Object

Returns:

the fully initialized MethodType object
readResolve
()

Resolves and initializes a MethodType object after serialization.

pack-priv MethodType

Returns:

the same type, except with the selected parameter(s) replaced
replaceParameterTypes
(int
the position (zero-based) of the first replaced parameter type(s)
start
,
int
the position (zero-based) after the last replaced parameter type(s)
end
,
Class<?>...
zero or more new parameter types to insert into the parameter list
ptypesToInsert
)

Finds or creates a method type with modified parameter types.

public Class<?>

Returns:

the return type
returnType
()

Implements java.lang.invoke.TypeDescriptor.OfMethod.returnType.

Returns the return type of this method type.

pack-priv Class<?>
pack-priv void
pack-priv static String
public String

Returns:

the descriptor string for this method type
toMethodDescriptorString
()

Returns the descriptor string for this method type.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the method type, of the form "(PT0,PT1...)RT".

public MethodType

Returns:

a version of the original type with all wrapper types replaced
unwrap
()

Converts all wrapper types to their corresponding primitive types.

private static MethodType
public MethodType

Returns:

a version of the original type with all primitive types replaced
wrap
()

Converts all primitive types to their corresponding wrapper types.

private static MethodType
private void
writeObject(ObjectOutputStream
the stream to write the object to
s
)

Save the MethodType instance to a stream.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait