public final class MethodType extends Object implements Serializable
MethodHandle.invokeExact
and MethodHandle.invokeGeneric
, 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 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 a CONSTANT_Class
.)
This loading may occur at any time before the MethodType
object is first derived.
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 to
Object . |
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, to
Object . |
static MethodType |
genericMethodType(int objectArgCount)
Finds or creates a method type whose components are all
Object . |
static MethodType |
genericMethodType(int objectArgCount,
boolean varargs)
Finds or creates a method type whose components are
Object with an optional trailing Object[] 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.
|
public static MethodType methodType(Class<?> rtype, Class<?>[] ptypes)
rtype
- the return typeptypes
- the parameter typesNullPointerException
- if rtype
or ptypes
or any element of ptypes
is nullIllegalArgumentException
- if any element of ptypes
is void.class
public static MethodType methodType(Class<?> rtype, List<Class<?>> ptypes)
methodType
.NullPointerException
- if rtype
or ptypes
or any element of ptypes
is nullIllegalArgumentException
- if any element of ptypes
is void.class
public static MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes)
methodType
.
The leading parameter type is prepended to the remaining array.NullPointerException
- if rtype
or ptype0
or ptypes
or any element of ptypes
is nullIllegalArgumentException
- if ptype0
or ptypes
or any element of ptypes
is void.class
public static MethodType methodType(Class<?> rtype)
methodType
.
The resulting method has no parameter types.NullPointerException
- if rtype
is nullpublic static MethodType methodType(Class<?> rtype, Class<?> ptype0)
methodType
.
The resulting method has the single given parameter type.NullPointerException
- if rtype
or ptype0
is nullIllegalArgumentException
- if ptype0
is void.class
public static MethodType methodType(Class<?> rtype, MethodType ptypes)
methodType
.
The resulting method has the same parameter types as ptypes
,
and the specified return type.NullPointerException
- if rtype
or ptypes
is nullpublic static MethodType genericMethodType(int objectArgCount, boolean varargs)
Object
with an optional trailing Object[]
array.
Convenience method for methodType
.
All parameters and the return type will be Object
,
except the final varargs parameter if any, which will be Object[]
.objectArgCount
- number of parameters (excluding the varargs parameter if any)varargs
- whether there will be a varargs parameter, of type Object[]
IllegalArgumentException
- if objectArgCount
is negative or greater than 255genericMethodType(int)
public static MethodType genericMethodType(int objectArgCount)
Object
.
Convenience method for methodType
.
All parameters and the return type will be Object.objectArgCount
- number of parametersIllegalArgumentException
- if objectArgCount
is negative or greater than 255genericMethodType(int, boolean)
public MethodType changeParameterType(int num, Class<?> nptype)
methodType
.num
- the index (zero-based) of the parameter type to changenptype
- a new parameter type to replace the old one withIndexOutOfBoundsException
- if num
is not a valid index into parameterArray()
IllegalArgumentException
- if nptype
is void.class
NullPointerException
- if nptype
is nullpublic MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert)
methodType
.num
- the position (zero-based) of the inserted parameter type(s)ptypesToInsert
- zero or more new parameter types to insert into the parameter listIndexOutOfBoundsException
- if num
is negative or greater than parameterCount()
IllegalArgumentException
- if any element of ptypesToInsert
is void.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- if ptypesToInsert
or any of its elements is nullpublic MethodType appendParameterTypes(Class<?>... ptypesToInsert)
methodType
.ptypesToInsert
- zero or more new parameter types to insert after the end of the parameter listIllegalArgumentException
- if any element of ptypesToInsert
is void.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- if ptypesToInsert
or any of its elements is nullpublic MethodType insertParameterTypes(int num, List<Class<?>> ptypesToInsert)
methodType
.num
- the position (zero-based) of the inserted parameter type(s)ptypesToInsert
- zero or more new parameter types to insert into the parameter listIndexOutOfBoundsException
- if num
is negative or greater than parameterCount()
IllegalArgumentException
- if any element of ptypesToInsert
is void.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- if ptypesToInsert
or any of its elements is nullpublic MethodType appendParameterTypes(List<Class<?>> ptypesToInsert)
methodType
.ptypesToInsert
- zero or more new parameter types to insert after the end of the parameter listIllegalArgumentException
- if any element of ptypesToInsert
is void.class
or if the resulting method type would have more than 255 parameter slotsNullPointerException
- if ptypesToInsert
or any of its elements is nullpublic MethodType dropParameterTypes(int start, int end)
methodType
.start
- the index (zero-based) of the first parameter type to removeend
- the index (greater than start
) of the first parameter type after not to removeIndexOutOfBoundsException
- if start
is negative or greater than parameterCount()
or if end
is negative or greater than parameterCount()
or if start
is greater than end
public MethodType changeReturnType(Class<?> nrtype)
methodType
.nrtype
- a return parameter type to replace the old one withNullPointerException
- if nrtype
is nullpublic boolean hasPrimitives()
void
counts as a primitive.public boolean hasWrappers()
Integer
.
The reference type java.lang.Void
counts as a wrapper.public MethodType erase()
Object
.
Convenience method for methodType
.
All primitive types (including void
) will remain unchanged.public MethodType generic()
Object
.
Convenience method for genericMethodType
.
The expression type.wrap().erase()
produces the same value
as type.generic()
.public MethodType wrap()
methodType
.
All reference types (including wrapper types) will remain unchanged.
A void
return type is changed to the type java.lang.Void
.
The expression type.wrap().erase()
produces the same value
as type.generic()
.public MethodType unwrap()
methodType
.
All primitive types (including void
) will remain unchanged.
A return type of java.lang.Void
is changed to void
.public Class<?> parameterType(int num)
num
- the index (zero-based) of the desired parameter typeIndexOutOfBoundsException
- if num
is not a valid index into parameterArray()
public int parameterCount()
public Class<?> returnType()
public List<Class<?>> parameterList()
public Class<?>[] parameterArray()
public boolean equals(Object x)
equals
in class Object
x
- object to comparetrue
if this object is the same as the obj
argument; false
otherwise.Object.equals(Object)
public int hashCode()
hashCode
in class Object
Object.hashCode()
,
equals(Object)
,
List.hashCode()
public String toString()
"(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
.
public static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader) throws IllegalArgumentException, TypeNotPresentException
methodType
.
Any class or interface name embedded in the descriptor string
will be resolved by calling ClassLoader.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
.
descriptor
- a bytecode-level type descriptor string "(T...)T"loader
- the class loader in which to look up the typesIllegalArgumentException
- if the string is not well-formedTypeNotPresentException
- if a named type cannot be foundpublic String toMethodDescriptorString()
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.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2011, Oracle and/or its affiliates. All rights reserved.
DRAFT ea-b138