Home » openjdk-7 » java.lang.invoke » [javadoc | source]
java.lang.invoke
public final class: MethodType [javadoc | source]
java.lang.Object
   java.lang.invoke.MethodType

All Implemented Interfaces:
    java$io$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 to MethodHandle.invokeExact and MethodHandle.invoke , and during execution of {@code invokedynamic} instructions.

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

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

{@code MethodType} objects are sometimes derived from bytecode instructions such as {@code 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 {@code ldc} instruction which refers to a suitable {@code CONSTANT_MethodType} constant pool entry. The entry refers to a {@code CONSTANT_Utf8} spelling for the descriptor string. For more details, see the package summary.

When the JVM materializes a {@code 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 {@code CONSTANT_Class}.) This loading may occur at any time before the {@code MethodType} object is first derived.

Field Summary
static final  HashMap<MethodType, MethodType> internTable     
static final  Class<?>[] NO_PTYPES     
Method from java.lang.invoke.MethodType Summary:
appendParameterTypes,   appendParameterTypes,   canConvert,   changeParameterType,   changeReturnType,   dropParameterTypes,   equals,   erase,   form,   fromMethodDescriptorString,   generic,   genericMethodType,   genericMethodType,   hasPrimitives,   hasWrappers,   hashCode,   insertParameterTypes,   insertParameterTypes,   invokers,   isConvertibleTo,   makeImpl,   methodType,   methodType,   methodType,   methodType,   methodType,   methodType,   parameterArray,   parameterCount,   parameterList,   parameterSlotCount,   parameterSlotDepth,   parameterType,   ptypes,   returnSlotCount,   returnType,   rtype,   toMethodDescriptorString,   toString,   unwrap,   wrap
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.lang.invoke.MethodType Detail:
 public MethodType appendParameterTypes(Class<?> ptypesToInsert) 
    Finds or creates a method type with additional parameter types. Convenience method for methodType .
 public MethodType appendParameterTypes(List<?> ptypesToInsert) 
    Finds or creates a method type with additional parameter types. Convenience method for methodType .
 static boolean canConvert(Class<?> src,
    Class<?> dst) 
 public MethodType changeParameterType(int num,
    Class<?> nptype) 
    Finds or creates a method type with a single different parameter type. Convenience method for methodType .
 public MethodType changeReturnType(Class<?> nrtype) 
    Finds or creates a method type with a different return type. Convenience method for methodType .
 public MethodType dropParameterTypes(int start,
    int end) 
    Finds or creates a method type with some parameter types omitted. Convenience method for methodType .
 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.
 public MethodType erase() 
    Erases all reference types to {@code Object}. Convenience method for methodType . All primitive types (including {@code void}) will remain unchanged.
 MethodTypeForm form() 
 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 for 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 {@code invokedynamic}.

 public MethodType generic() 
    Converts all types, both reference and primitive, to {@code Object}. Convenience method for genericMethodType . The expression {@code type.wrap().erase()} produces the same value as {@code type.generic()}.
 public static MethodType genericMethodType(int objectArgCount) 
    Finds or creates a method type whose components are all {@code Object}. Convenience method for methodType . All parameters and the return type will be Object.
 public static MethodType genericMethodType(int objectArgCount,
    boolean finalArray) 
    Finds or creates a method type whose components are {@code Object} with an optional trailing {@code Object[]} array. Convenience method for methodType . All parameters and the return type will be {@code Object}, except the final array parameter if any, which will be {@code Object[]}.
 public boolean hasPrimitives() 
    Reports if this type contains a primitive argument or return value. The return type {@code void} counts as a primitive.
 public boolean hasWrappers() 
    Reports if this type contains a wrapper argument or return value. Wrappers are types which box primitive values, such as Integer . The reference type {@code java.lang.Void} counts as a wrapper, if it occurs as a return type.
 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.
 public MethodType insertParameterTypes(int num,
    Class<?> ptypesToInsert) 
    Finds or creates a method type with additional parameter types. Convenience method for methodType .
 public MethodType insertParameterTypes(int num,
    List<?> ptypesToInsert) 
    Finds or creates a method type with additional parameter types. Convenience method for methodType .
 Invokers invokers() 
 boolean isConvertibleTo(MethodType newType) 
 static MethodType makeImpl(Class<?> rtype,
    Class<?>[] ptypes,
    boolean trusted) 
    Sole factory method to find or create an interned method type.
 public static MethodType methodType(Class<?> rtype) 
    Finds or creates a method type with the given components. Convenience method for methodType . The resulting method has no parameter types.
 public static MethodType methodType(Class<?> rtype,
    Class<?>[] ptypes) 
    Finds or creates an instance of the given method type.
 public static MethodType methodType(Class<?> rtype,
    List<?> ptypes) 
    Finds or creates a method type with the given components. Convenience method for methodType .
 public static MethodType methodType(Class<?> rtype,
    Class<?> ptype0) 
    Finds or creates a method type with the given components. Convenience method for methodType . The resulting method has the single given parameter type.
 public static MethodType methodType(Class<?> rtype,
    MethodType ptypes) 
    Finds or creates a method type with the given components. Convenience method for methodType . The resulting method has the same parameter types as {@code ptypes}, and the specified return type.
 public static MethodType methodType(Class<?> rtype,
    Class<?> ptype0,
    Class<?> ptypes) 
    Finds or creates a method type with the given components. Convenience method for methodType . The leading parameter type is prepended to the remaining array.
 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.
 public int parameterCount() 
    Returns the number of parameter types in this method type.
 public List<?> parameterList() 
    Presents the parameter types as a list (a convenience method). The list will be immutable.
 int parameterSlotCount() 
    Reports the number of JVM stack slots required to invoke a method of this type. Note that (for historical 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.

 int parameterSlotDepth(int num) 
    Reports the number of JVM stack slots which carry all parameters including and after the given position, which must be in the range of 0 to {@code 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 {@code N}, obtain the {@code parameterSlotDepth} of its trailing edge at position {@code N+1}.

    Parameters of type {@code long} and {@code 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.

 public Class<?> parameterType(int num) 
    Returns the parameter type at the specified index, within this method type.
 Class<?>[] ptypes() 
 int returnSlotCount() 
    Reports 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.

 public Class<?> returnType() 
    Returns the return type of this method type.
 Class<?> rtype() 
 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 {@code invokedynamic}. fromMethodDescriptorString , because the latter requires a suitable class loader argument.

 public String toString() 
    Returns a string representation of the method type, of the form {@code "(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 MethodType unwrap() 
    Converts all wrapper types to their corresponding primitive types. Convenience method for methodType . All primitive types (including {@code void}) will remain unchanged. A return type of {@code java.lang.Void} is changed to {@code void}.
 public MethodType wrap() 
    Converts all primitive types to their corresponding wrapper types. Convenience method for methodType . All reference types (including wrapper types) will remain unchanged. A {@code void} return type is changed to the type {@code java.lang.Void}. The expression {@code type.wrap().erase()} produces the same value as {@code type.generic()}.