- java.lang.Object
-
- java.lang.invoke.MethodType
-
- All Implemented Interfaces:
- Serializable
public final class MethodType extends Object implements 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 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
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 an
ldc
instruction which refers to a suitableCONSTANT_MethodType
constant pool entry. The entry refers to aCONSTANT_Utf8
spelling for the descriptor string. For more details, see the package summary.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 aCONSTANT_Class
.) This loading may occur at any time before theMethodType
object is first derived.- See Also:
- Serialized Form
-
-
Method Summary
Methods Modifier and Type Method and Description MethodType
appendParameterTypes(Class<?>... ptypesToInsert)
Finds or creates a method type with additional parameter types.MethodType
appendParameterTypes(List<Class<?>> ptypesToInsert)
Finds or creates a method type with additional parameter types.MethodType
changeParameterType(int num, Class<?> nptype)
Finds or creates a method type with a single different parameter type.MethodType
changeReturnType(Class<?> nrtype)
Finds or creates a method type with a different return type.MethodType
dropParameterTypes(int start, int end)
Finds or creates a method type with some parameter types omitted.boolean
equals(Object x)
Compares the specified object with this type for equality.MethodType
erase()
Erases all reference types toObject
.static MethodType
fromMethodDescriptorString(String descriptor, ClassLoader loader)
Finds or creates an instance of a method type, given the spelling of its bytecode descriptor.MethodType
generic()
Converts all types, both reference and primitive, toObject
.static MethodType
genericMethodType(int objectArgCount)
Finds or creates a method type whose components are allObject
.static MethodType
genericMethodType(int objectArgCount, boolean finalArray)
Finds or creates a method type whose components areObject
with an optional trailingObject[]
array.int
hashCode()
Returns the hash code value for this method type.boolean
hasPrimitives()
Reports if this type contains a primitive argument or return value.boolean
hasWrappers()
Reports if this type contains a wrapper argument or return value.MethodType
insertParameterTypes(int num, Class<?>... ptypesToInsert)
Finds or creates a method type with additional parameter types.MethodType
insertParameterTypes(int num, List<Class<?>> ptypesToInsert)
Finds or creates a method type with additional parameter types.static MethodType
methodType(Class<?> rtype)
Finds or creates a method type with the given components.static MethodType
methodType(Class<?> rtype, Class<?> ptype0)
Finds or creates a method type with the given components.static MethodType
methodType(Class<?> rtype, Class<?>[] ptypes)
Finds or creates an instance of the given method type.static MethodType
methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes)
Finds or creates a method type with the given components.static MethodType
methodType(Class<?> rtype, List<Class<?>> ptypes)
Finds or creates a method type with the given components.static MethodType
methodType(Class<?> rtype, MethodType ptypes)
Finds or creates a method type with the given components.Class<?>[]
parameterArray()
Presents the parameter types as an array (a convenience method).int
parameterCount()
Returns the number of parameter types in this method type.List<Class<?>>
parameterList()
Presents the parameter types as a list (a convenience method).Class<?>
parameterType(int num)
Returns the parameter type at the specified index, within this method type.Class<?>
returnType()
Returns the return type of this method type.String
toMethodDescriptorString()
Produces a bytecode descriptor representation of the method type.String
toString()
Returns a string representation of the method type, of the form"(PT0,PT1...)RT"
.MethodType
unwrap()
Converts all wrapper types to their corresponding primitive types.MethodType
wrap()
Converts all primitive types to their corresponding wrapper types.
-
-
-
Method Detail
-
methodType
public static MethodType methodType(Class<?> rtype, Class<?>[] ptypes)
Finds or creates an instance of the given method type.- Parameters:
rtype
- the return typeptypes
- the parameter types- Returns:
- a method type with the given components
- Throws:
NullPointerException
- ifrtype
orptypes
or any element ofptypes
is nullIllegalArgumentException
- if any element ofptypes
isvoid.class
-
methodType
public static MethodType methodType(Class<?> rtype, List<Class<?>> ptypes)
Finds or creates a method type with the given components. Convenience method formethodType
.- Returns:
- a method type with the given components
- Throws:
NullPointerException
- ifrtype
orptypes
or any element ofptypes
is nullIllegalArgumentException
- if any element ofptypes
isvoid.class
-
methodType
public static MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes)
Finds or creates a method type with the given components. Convenience method formethodType
. The leading parameter type is prepended to the remaining array.- Returns:
- a method type with the given components
- Throws:
NullPointerException
- ifrtype
orptype0
orptypes
or any element ofptypes
is nullIllegalArgumentException
- ifptype0
orptypes
or any element ofptypes
isvoid.class
-
methodType
public static MethodType methodType(Class<?> rtype)
Finds or creates a method type with the given components. Convenience method formethodType
. The resulting method has no parameter types.- Returns:
- a method type with the given return value
- Throws:
NullPointerException
- ifrtype
is null
-
methodType
public static MethodType methodType(Class<?> rtype, Class<?> ptype0)
Finds or creates a method type with the given components. Convenience method formethodType
. The resulting method has the single given parameter type.- Returns:
- a method type with the given return value and parameter type
- Throws:
NullPointerException
- ifrtype
orptype0
is nullIllegalArgumentException
- ifptype0
isvoid.class
-
methodType
public static MethodType methodType(Class<?> rtype, MethodType ptypes)
Finds or creates a method type with the given components. Convenience method formethodType
. The resulting method has the same parameter types asptypes
, and the specified return type.- Throws:
NullPointerException
- ifrtype
orptypes
is null
-
genericMethodType
public static MethodType genericMethodType(int objectArgCount, boolean finalArray)
Finds or creates a method type whose components areObject
with an optional trailingObject[]
array. Convenience method formethodType
. All parameters and the return type will beObject
, except the final array parameter if any, which will beObject[]
.- Parameters:
objectArgCount
- number of parameters (excluding the final array parameter if any)finalArray
- whether there will be a trailing array parameter, of typeObject[]
- Returns:
- a generally applicable method type, for all calls of the given fixed argument count and a collected array of further arguments
- Throws:
IllegalArgumentException
- ifobjectArgCount
is negative or greater than 255 (or 254, iffinalArray
is true)- See Also:
genericMethodType(int)
-
genericMethodType
public static MethodType genericMethodType(int objectArgCount)
Finds or creates a method type whose components are allObject
. Convenience method formethodType
. All parameters and the return type will be Object.- Parameters:
objectArgCount
- number of parameters- Returns:
- a generally applicable method type, for all calls of the given argument count
- Throws:
IllegalArgumentException
- ifobjectArgCount
is negative or greater than 255- See Also:
genericMethodType(int, boolean)
-
changeParameterType
public MethodType changeParameterType(int num, Class<?> nptype)
Finds or creates a method type with a single different parameter type. Convenience method formethodType
.- Parameters:
num
- the index (zero-based) of the parameter type to changenptype
- a new parameter type to replace the old one with- Returns:
- the same type, except with the selected parameter changed
- Throws:
IndexOutOfBoundsException
- ifnum
is not a valid index intoparameterArray()
IllegalArgumentException
- ifnptype
isvoid.class
NullPointerException
- ifnptype
is null
-
insertParameterTypes
public MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert)
Finds or creates a method type with additional parameter types. Convenience method formethodType
.- Parameters:
num
- the position (zero-based) of the inserted parameter type(s)ptypesToInsert
- zero or more new parameter types to insert into the parameter list- Returns:
- the same type, except with the selected parameter(s) inserted
- Throws:
IndexOutOfBoundsException
- ifnum
is negative or greater thanparameterCount()
IllegalArgumentException
- if any element ofptypesToInsert
isvoid.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- ifptypesToInsert
or any of its elements is null
-
appendParameterTypes
public MethodType appendParameterTypes(Class<?>... ptypesToInsert)
Finds or creates a method type with additional parameter types. Convenience method formethodType
.- Parameters:
ptypesToInsert
- zero or more new parameter types to insert after the end of the parameter list- Returns:
- the same type, except with the selected parameter(s) appended
- Throws:
IllegalArgumentException
- if any element ofptypesToInsert
isvoid.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- ifptypesToInsert
or any of its elements is null
-
insertParameterTypes
public MethodType insertParameterTypes(int num, List<Class<?>> ptypesToInsert)
Finds or creates a method type with additional parameter types. Convenience method formethodType
.- Parameters:
num
- the position (zero-based) of the inserted parameter type(s)ptypesToInsert
- zero or more new parameter types to insert into the parameter list- Returns:
- the same type, except with the selected parameter(s) inserted
- Throws:
IndexOutOfBoundsException
- ifnum
is negative or greater thanparameterCount()
IllegalArgumentException
- if any element ofptypesToInsert
isvoid.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- ifptypesToInsert
or any of its elements is null
-
appendParameterTypes
public MethodType appendParameterTypes(List<Class<?>> ptypesToInsert)
Finds or creates a method type with additional parameter types. Convenience method formethodType
.- Parameters:
ptypesToInsert
- zero or more new parameter types to insert after the end of the parameter list- Returns:
- the same type, except with the selected parameter(s) appended
- Throws:
IllegalArgumentException
- if any element ofptypesToInsert
isvoid.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- ifptypesToInsert
or any of its elements is null
-
dropParameterTypes
public MethodType dropParameterTypes(int start, int end)
Finds or creates a method type with some parameter types omitted. Convenience method formethodType
.- Parameters:
start
- the index (zero-based) of the first parameter type to removeend
- the index (greater thanstart
) of the first parameter type after not to remove- Returns:
- the same type, except with the selected parameter(s) removed
- Throws:
IndexOutOfBoundsException
- ifstart
is negative or greater thanparameterCount()
or ifend
is negative or greater thanparameterCount()
or ifstart
is greater thanend
-
changeReturnType
public MethodType changeReturnType(Class<?> nrtype)
Finds or creates a method type with a different return type. Convenience method formethodType
.- Parameters:
nrtype
- a return parameter type to replace the old one with- Returns:
- the same type, except with the return type change
- Throws:
NullPointerException
- ifnrtype
is null
-
hasPrimitives
public boolean hasPrimitives()
Reports if this type contains a primitive argument or return value. The return typevoid
counts as a primitive.- Returns:
- true if any of the types are primitives
-
hasWrappers
public boolean hasWrappers()
Reports if this type contains a wrapper argument or return value. Wrappers are types which box primitive values, such asInteger
. The reference typejava.lang.Void
counts as a wrapper, if it occurs as a return type.- Returns:
- true if any of the types are wrappers
-
erase
public MethodType erase()
Erases all reference types toObject
. Convenience method formethodType
. All primitive types (includingvoid
) will remain unchanged.- Returns:
- a version of the original type with all reference types replaced
-
generic
public MethodType generic()
Converts all types, both reference and primitive, toObject
. Convenience method forgenericMethodType
. The expressiontype.wrap().erase()
produces the same value astype.generic()
.- Returns:
- a version of the original type with all types replaced
-
wrap
public MethodType wrap()
Converts all primitive types to their corresponding wrapper types. Convenience method formethodType
. All reference types (including wrapper types) will remain unchanged. Avoid
return type is changed to the typejava.lang.Void
. The expressiontype.wrap().erase()
produces the same value astype.generic()
.- Returns:
- a version of the original type with all primitive types replaced
-
unwrap
public MethodType unwrap()
Converts all wrapper types to their corresponding primitive types. Convenience method formethodType
. All primitive types (includingvoid
) will remain unchanged. A return type ofjava.lang.Void
is changed tovoid
.- Returns:
- a version of the original type with all wrapper types replaced
-
parameterType
public Class<?> parameterType(int num)
Returns the parameter type at the specified index, within this method type.- Parameters:
num
- the index (zero-based) of the desired parameter type- Returns:
- the selected parameter type
- Throws:
IndexOutOfBoundsException
- ifnum
is not a valid index intoparameterArray()
-
parameterCount
public int parameterCount()
Returns the number of parameter types in this method type.- Returns:
- the number of parameter types
-
returnType
public Class<?> returnType()
Returns the return type of this method type.- Returns:
- the return type
-
parameterList
public List<Class<?>> parameterList()
Presents the parameter types as a list (a convenience method). The list will be immutable.- Returns:
- the parameter types (as an immutable list)
-
parameterArray
public Class<?>[] parameterArray()
Presents the parameter types as an array (a convenience method). Changes to the array will not result in changes to the type.- Returns:
- the parameter types (as a fresh copy if necessary)
-
equals
public boolean equals(Object x)
Compares the specified object with this type for equality. That is, it returns true if and only if the specified object is also a method type with exactly the same parameters and return type.- Overrides:
equals
in classObject
- Parameters:
x
- object to compare- Returns:
true
if this object is the same as the obj argument;false
otherwise.- See Also:
Object.equals(Object)
-
hashCode
public int hashCode()
Returns the hash code value for this method type. It is defined to be the same as the hashcode of a List whose elements are the return type followed by the parameter types.- Overrides:
hashCode
in classObject
- Returns:
- the hash code value for this method type
- See Also:
Object.hashCode()
,equals(Object)
,List.hashCode()
-
toString
public String toString()
Returns a string representation of the method type, of the form"(PT0,PT1...)RT"
. The string representation of a method type is a parenthesis enclosed, comma separated list of type names, followed immediately by the return type.Each type is represented by its
simple name
.
-
fromMethodDescriptorString
public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader) throws IllegalArgumentException, TypeNotPresentException
Finds or creates an instance of a method type, given the spelling of its bytecode descriptor. Convenience method formethodType
. Any class or interface name embedded in the descriptor string will be resolved by callingClassLoader.loadClass(java.lang.String)
on the given loader (or if it is null, on the system class loader).Note that it is possible to encounter method types which cannot be constructed by this method, because their component types are not all reachable from a common class loader.
This method is included for the benfit of applications that must generate bytecodes that process method handles and
invokedynamic
.- Parameters:
descriptor
- a bytecode-level type descriptor string "(T...)T"loader
- the class loader in which to look up the types- Returns:
- a method type matching the bytecode-level type descriptor
- Throws:
NullPointerException
- if the string is nullIllegalArgumentException
- if the string is not well-formedTypeNotPresentException
- if a named type cannot be found
-
toMethodDescriptorString
public String toMethodDescriptorString()
Produces a bytecode descriptor representation of the method type.Note that this is not a strict inverse of
fromMethodDescriptorString
. Two distinct classes which share a common name but have different class loaders will appear identical when viewed within descriptor strings.This method is included for the benfit of applications that must generate bytecodes that process method handles and
invokedynamic
.fromMethodDescriptorString
, because the latter requires a suitable class loader argument.- Returns:
- the bytecode type descriptor representation
-
-
Traduction non disponible
Les API Java ne sont pas encore traduites en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Version en cache
21/12/2024 14:31:23 Cette version de la page est en cache (à la date du 21/12/2024 14:31:23) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 11/06/2005, dernière modification le 04/03/2020
Source du document imprimé : https://www.gaudry.be/java-api-rf-java/lang/invoke/MethodType.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.