Please note that this documentation is not final and is subject to change.

Java™ Platform
Standard Ed. 7

DRAFT ea-b76

java.dyn
Class MethodType

java.lang.Object
  extended by java.dyn.MethodType

public final class MethodType
extends Object

Run-time token used to match call sites with method handles. The structure is a return type accompanied by any number of parameter types. The types (primitive, void, and reference) are represented by Class objects. All instances of MethodType are immutable. Two instances are completely interchangeable if they compare equal. Equality depends exactly on the return and parameter types.

This type can be created only by factory methods, which manage interning.


Method Summary
Modifier and Type Method and Description
 MethodType changeParameterType(int num, Class<?> nptype)
          Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).
 MethodType changeReturnType(Class<?> nrtype)
          Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).
 MethodType dropParameterType(int num)
          Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).
 boolean equals(Object x)
          Compares the specified object with this type for equality.
 MethodType erase()
          Convenience method for make(java.lang.Class, java.lang.Class[]).
static MethodType fromBytecodeString(String bytecodeSignature, ClassLoader loader)
          Convenience method for make(java.lang.Class, java.lang.Class[]).
 MethodType generic()
          Convenience method for makeGeneric(int).
 int hashCode()
          Returns the hash code value for this method type.
 boolean hasPrimitives()
          Convenience method.
 boolean hasWrappers()
          Convenience method.
 MethodType insertParameterType(int num, Class<?> nptype)
          Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).
static MethodType make(Class<?> rtype)
          Convenience method for make(java.lang.Class, java.lang.Class[]).
static MethodType make(Class<?> rtype, Class<?> ptype0)
          Convenience method for make(java.lang.Class, java.lang.Class[]).
static MethodType make(Class<?> rtype, Class<?>[] ptypes)
          Find or create an instance of the given method type.
static MethodType make(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes)
          Convenience method for make(java.lang.Class, java.lang.Class[]).
static MethodType make(Class<?> rtype, List<? extends Class<?>> ptypes)
          Convenience method for make(java.lang.Class, java.lang.Class[]).
static MethodType make(Class<?> rtype, MethodType ptypes)
          Convenience method for make(java.lang.Class, java.lang.Class[]).
static MethodType makeGeneric(int objectArgCount)
          All parameters and the return type will be Object.
static MethodType makeGeneric(int objectArgCount, boolean varargs)
          Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).
 Class<?>[] parameterArray()
          Convenience method to present the arguments as an array.
 int parameterCount()
           
 List<Class<?>> parameterList()
          Convenience method to present the arguments as a list.
 int parameterSlotCount()
          The number of JVM stack slots required to invoke a method of this type.
 int parameterSlotDepth(int num)
          Number of JVM stack slots which carry all parameters after the given position, which must be in the range of 0 to parameterCount inclusive.
 Class<?> parameterType(int num)
           
 int returnSlotCount()
          The number of JVM stack slots required to receive a return value from a method of this type.
 Class<?> returnType()
           
 String toBytecodeString()
          Create a bytecode signature representation of the type.
 String toString()
          The string representation of a method type is a parenthesis enclosed, comma separated list of type names, followed immediately by the return type.
 MethodType unwrap()
          Convenience method for make(java.lang.Class, java.lang.Class[]).
 MethodType wrap()
          Convenience method for make(java.lang.Class, java.lang.Class[]).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

make

public static MethodType make(Class<?> rtype,
                              Class<?>[] ptypes)
Find or create an instance of the given method type.

Parameters:
rtype - the return type
ptypes - the parameter types
Returns:
the interned method type with the given parts
Throws:
NullPointerException - if rtype or any ptype is null
IllegalArgumentException - if any of the ptypes is void

make

public static MethodType make(Class<?> rtype,
                              List<? extends Class<?>> ptypes)
Convenience method for make(java.lang.Class, java.lang.Class[]).


make

public static MethodType make(Class<?> rtype,
                              Class<?> ptype0,
                              Class<?>... ptypes)
Convenience method for make(java.lang.Class, java.lang.Class[]). The leading parameter type is prepended to the remaining array.


make

public static MethodType make(Class<?> rtype)
Convenience method for make(java.lang.Class, java.lang.Class[]). The resulting method has no parameter types.


make

public static MethodType make(Class<?> rtype,
                              Class<?> ptype0)
Convenience method for make(java.lang.Class, java.lang.Class[]). The resulting method has the single given parameter type.


make

public static MethodType make(Class<?> rtype,
                              MethodType ptypes)
Convenience method for make(java.lang.Class, java.lang.Class[]). The resulting method has the same parameter types as ptypes, and the specified return type.


makeGeneric

public static MethodType makeGeneric(int objectArgCount,
                                     boolean varargs)
Convenience method for #make(java.lang.Class, java.lang.Class[], boolean). All parameters and the return type will be Object, except the final varargs parameter if any.

Parameters:
objectArgCount - number of parameters (excluding the varargs parameter if any)
varargs - whether there will be a varargs parameter, of type Object[]
Returns:
a totally generic method type, given only its count of parameters and varargs
See Also:
makeGeneric(int)

makeGeneric

public static MethodType makeGeneric(int objectArgCount)
All parameters and the return type will be Object.

Parameters:
objectArgCount - number of parameters
Returns:
a totally generic method type, given only its count of parameters
See Also:
makeGeneric(int, boolean)

changeParameterType

public MethodType changeParameterType(int num,
                                      Class<?> nptype)
Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).

Parameters:
num - the index (zero-based) of the parameter type to change
nptype - a new parameter type to replace the old one with
Returns:
the same type, except with the selected parameter changed

insertParameterType

public MethodType insertParameterType(int num,
                                      Class<?> nptype)
Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).

Parameters:
num - the position (zero-based) of the inserted parameter type
nptype - a new parameter type to insert into the parameter list
Returns:
the same type, except with the selected parameter inserted

dropParameterType

public MethodType dropParameterType(int num)
Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).

Parameters:
num - the index (zero-based) of the parameter type to remove
Returns:
the same type, except with the selected parameter removed

changeReturnType

public MethodType changeReturnType(Class<?> nrtype)
Convenience method for #make(java.lang.Class, java.lang.Class[], boolean).

Parameters:
nrtype - a return parameter type to replace the old one with
Returns:
the same type, except with the return type change

hasPrimitives

public boolean hasPrimitives()
Convenience method. Report if this type contains a primitive argument or return value.

Returns:
true if any of the types are primitives

hasWrappers

public boolean hasWrappers()
Convenience method. Report if this type contains a wrapper argument or return value. Wrappers are types which box primitive values, such as Integer.

Returns:
true if any of the types are wrappers

erase

public MethodType erase()
Convenience method for make(java.lang.Class, java.lang.Class[]). Erase all reference types to Object.

Returns:
a version of the original type with all reference types replaced

generic

public MethodType generic()
Convenience method for makeGeneric(int). Convert all types, both reference and primitive, to Object.

Returns:
a version of the original type with all types replaced

wrap

public MethodType wrap()
Convenience method for make(java.lang.Class, java.lang.Class[]). Convert all primitive types to their corresponding wrapper types. A void return type is changed to the type java.lang.Void.

Returns:
a version of the original type with all primitive types replaced

unwrap

public MethodType unwrap()
Convenience method for make(java.lang.Class, java.lang.Class[]). Convert all wrapper types to their corresponding primitive types. A return type of java.lang.Void is changed to void.

Returns:
a version of the original type with all wrapper types replaced

parameterType

public Class<?> parameterType(int num)
Parameters:
num - the index (zero-based) of the desired parameter type
Returns:
the selected parameter type

parameterCount

public int parameterCount()
Returns:
the number of parameter types

returnType

public Class<?> returnType()
Returns:
the return type

parameterList

public List<Class<?>> parameterList()
Convenience method to present the arguments as a list.

Returns:
the parameter types (as an immutable list)

parameterArray

public Class<?>[] parameterArray()
Convenience method to present the arguments as an array.

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 class Object
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 class Object
Returns:
the hash code value for this method type
See Also:
Object.hashCode(), equals(Object), List.hashCode()

toString

public String toString()
The string representation of a method type is a parenthesis enclosed, comma separated list of type names, followed immediately by the return type.

If a type name is array, it the base type followed by [], rather than the Class.getName of the array type.

Overrides:
toString in class Object
Returns:
a string representation of the object.

parameterSlotCount

public int parameterSlotCount()
The number of JVM stack slots required to invoke a method of this type. Note that (for historic reasons) the JVM requires a second stack slot to pass long and double arguments. So this method returns parameterCount() plus the number of long and double parameters (if any).

This method is included for the benfit of applications that must generate bytecodes that process method handles and invokedynamic.

Returns:
the number of JVM stack slots for this type's parameters

parameterSlotDepth

public int parameterSlotDepth(int num)
Number of JVM stack slots which carry all parameters after the given position, which must be in the range of 0 to parameterCount inclusive. Successive parameters are more shallowly stacked, and parameters are indexed in the bytecodes according to their trailing edge. Thus, to obtain the depth in the outgoing call stack of parameter N, obtain the parameterSlotDepth of its trailing edge at position N+1.

Parameters of type long and double occupy two stack slots (for historical reasons) and all others occupy one. Therefore, the number returned is the number of arguments including and after the given parameter, plus the number of long or double arguments at or after after the argument for the given parameter.

This method is included for the benfit of applications that must generate bytecodes that process method handles and invokedynamic.

Parameters:
num - an index (zero-based, inclusive) within the parameter types
Returns:
the index of the (shallowest) JVM stack slot transmitting the given parameter

returnSlotCount

public int returnSlotCount()
The number of JVM stack slots required to receive a return value from a method of this type. If the return type is void, it will be zero, else if the return type is long or double, it will be two, else one.

This method is included for the benfit of applications that must generate bytecodes that process method handles and invokedynamic.

Returns:
the number of JVM stack slots (0, 1, or 2) for this type's return value

fromBytecodeString

public static MethodType fromBytecodeString(String bytecodeSignature,
                                            ClassLoader loader)
                                     throws IllegalArgumentException,
                                            TypeNotPresentException
Convenience method for make(java.lang.Class, java.lang.Class[]). Find or create an instance (interned) of the given method type. Any class or interface name embedded in the signature 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 build 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:
bytecodeSignature - a bytecode-level signature string "(T...)T"
loader - the class loader in which to look up the types
Returns:
a method type matching the bytecode-level signature
Throws:
IllegalArgumentException - if the string is not well-formed
TypeNotPresentException - if a named type cannot be found

toBytecodeString

public String toBytecodeString()
Create a bytecode signature representation of the type. Note that this is not a strict inverse of

This method is included for the benfit of applications that must generate bytecodes that process method handles and invokedynamic. fromBytecodeString(java.lang.String, java.lang.ClassLoader), because the latter requires a suitable class loader argument.

Returns:
the bytecode signature representation

Java™ Platform
Standard Ed. 7

DRAFT ea-b76

Submit a bug or feature

Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms.