Spec-Zone .ru
спецификации, руководства, описания, API
Please note that the specifications and other information contained herein are not final and are subject to change. The information is being made available to you solely for purpose of evaluation.

Java™ Platform
Standard Ed. 7

DRAFT ea-b118

java.dyn
Class MethodType

java.lang.Object
  extended by java.dyn.MethodType
All Implemented Interfaces:
Type

public final class MethodType
extends Object
implements Type

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(A...) and MethodHandle.invokeGeneric(A...), 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.

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. When this occurs, any classes named in the descriptor strings must be loaded. (But they need not be initialized.) This loading may occur at any time before the MethodType object is first derived.

Like classes and strings, method types can be represented directly in a class file's constant pool as constants to be loaded by ldc bytecodes. Loading such a constant causes its component classes to be loaded as necessary.


Method Summary
Modifier and Type Method and Description
 MethodType changeParameterType(int num, Class<?> nptype)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 MethodType changeReturnType(Class<?> nrtype)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 MethodType dropParameterType(int num)
          Deprecated. Use dropParameterTypes(int, int) instead.
 MethodType dropParameterTypes(int start, int end)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 boolean equals(Object x)
          Compares the specified object with this type for equality.
 MethodType erase()
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
static MethodType fromBytecodeString(String descriptor, ClassLoader loader)
          Temporary alias for fromMethodDescriptorString; delete after M3.
static MethodType fromMethodDescriptorString(String descriptor, ClassLoader loader)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 MethodType generic()
          Convenience method for genericMethodType(int).
static MethodType genericMethodType(int objectArgCount)
          All parameters and the return type will be Object.
static MethodType genericMethodType(int objectArgCount, boolean varargs)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 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)
          Deprecated. Use insertParameterTypes(int, java.lang.Class<?>...) instead.
 MethodType insertParameterTypes(int num, Class<?>... ptypesToInsert)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 MethodType insertParameterTypes(int num, List<Class<?>> ptypesToInsert)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
static MethodType make(Class<?> rtype)
          Deprecated. 
static MethodType make(Class<?> rtype, Class<?> ptype0)
          Deprecated. 
static MethodType make(Class<?> rtype, Class<?>[] ptypes)
          Deprecated. 
static MethodType make(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes)
          Deprecated. 
static MethodType make(Class<?> rtype, List<? extends Class<?>> ptypes)
          Deprecated. 
static MethodType make(Class<?> rtype, MethodType ptypes)
          Deprecated. 
static MethodType makeGeneric(int objectArgCount)
          Deprecated. 
static MethodType makeGeneric(int objectArgCount, boolean varargs)
          Deprecated. 
static MethodType methodType(Class<?> rtype)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
static MethodType methodType(Class<?> rtype, Class<?> ptype0)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
static MethodType methodType(Class<?> rtype, Class<?>[] ptypes)
          Find or create an instance of the given method type.
static MethodType methodType(Class<?> rtype, Class<?> ptype0, Class<?>... ptypes)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
static MethodType methodType(Class<?> rtype, List<? extends Class<?>> ptypes)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
static MethodType methodType(Class<?> rtype, MethodType ptypes)
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 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 including and 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()
          Temporary alias for toMethodDescriptorString; delete after M3.
 String toMethodDescriptorString()
          Create a bytecode descriptor representation of the method 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 methodType(java.lang.Class, java.lang.Class[]).
 MethodType wrap()
          Convenience method for methodType(java.lang.Class, java.lang.Class[]).
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

methodType

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

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

make

@Deprecated
public static MethodType make(Class<?> rtype,
                                         Class<?>[] ptypes)
Deprecated. 


methodType

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


make

@Deprecated
public static MethodType make(Class<?> rtype,
                                         List<? extends Class<?>> ptypes)
Deprecated. 


methodType

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


make

@Deprecated
public static MethodType make(Class<?> rtype,
                                         Class<?> ptype0,
                                         Class<?>... ptypes)
Deprecated. 


methodType

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


make

@Deprecated
public static MethodType make(Class<?> rtype)
Deprecated. 


methodType

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


make

@Deprecated
public static MethodType make(Class<?> rtype,
                                         Class<?> ptype0)
Deprecated. 


methodType

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


make

@Deprecated
public static MethodType make(Class<?> rtype,
                                         MethodType ptypes)
Deprecated. 


genericMethodType

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

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:
genericMethodType(int)

makeGeneric

@Deprecated
public static MethodType makeGeneric(int objectArgCount,
                                                boolean varargs)
Deprecated. 


genericMethodType

public static MethodType genericMethodType(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:
genericMethodType(int, boolean)

makeGeneric

@Deprecated
public static MethodType makeGeneric(int objectArgCount)
Deprecated. 


changeParameterType

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

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

@Deprecated
public MethodType insertParameterType(int num,
                                                 Class<?> nptype)
Deprecated. Use insertParameterTypes(int, java.lang.Class<?>...) instead.

Convenience method for insertParameterTypes(int, java.lang.Class<?>...).


insertParameterTypes

public MethodType insertParameterTypes(int num,
                                       Class<?>... ptypesToInsert)
Convenience method for methodType(java.lang.Class, java.lang.Class[]).

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

insertParameterTypes

public MethodType insertParameterTypes(int num,
                                       List<Class<?>> ptypesToInsert)
Convenience method for methodType(java.lang.Class, java.lang.Class[]).

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

dropParameterTypes

public MethodType dropParameterTypes(int start,
                                     int end)
Convenience method for methodType(java.lang.Class, java.lang.Class[]).

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

dropParameterType

@Deprecated
public MethodType dropParameterType(int num)
Deprecated. Use dropParameterTypes(int, int) instead.

Convenience method for dropParameterTypes(int, int).


changeReturnType

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

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. The return type void counts as a primitive.

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. The reference type java.lang.Void counts as a wrapper.

Returns:
true if any of the types are wrappers

erase

public MethodType erase()
Convenience method for methodType(java.lang.Class, java.lang.Class[]). Erase all reference types to Object. All primitive types (including void) will remain unchanged.

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

generic

public MethodType generic()
Convenience method for genericMethodType(int). Convert all types, both reference and primitive, to Object. The expression type.wrap().erase() produces the same value as type.generic().

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

wrap

public MethodType wrap()
Convenience method for methodType(java.lang.Class, java.lang.Class[]). Convert all primitive types to their corresponding wrapper types. 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().

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

unwrap

public MethodType unwrap()
Convenience method for methodType(java.lang.Class, java.lang.Class[]). Convert all wrapper types to their corresponding primitive types. All primitive types (including void) will remain unchanged. 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. 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 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 including and 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

fromMethodDescriptorString

public static MethodType fromMethodDescriptorString(String descriptor,
                                                    ClassLoader loader)
                                             throws IllegalArgumentException,
                                                    TypeNotPresentException
Convenience method for methodType(java.lang.Class, java.lang.Class[]). Find or create an instance of the given method type. 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.

Parameters:
descriptor - 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

toMethodDescriptorString

public String toMethodDescriptorString()
Create a bytecode descriptor representation of the method type.

Note that this is not a strict inverse of fromMethodDescriptorString(java.lang.String, java.lang.ClassLoader). 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(java.lang.String, java.lang.ClassLoader), because the latter requires a suitable class loader argument.

Returns:
the bytecode signature representation

toBytecodeString

public String toBytecodeString()
Temporary alias for toMethodDescriptorString; delete after M3.


fromBytecodeString

public static MethodType fromBytecodeString(String descriptor,
                                            ClassLoader loader)
                                     throws IllegalArgumentException,
                                            TypeNotPresentException
Temporary alias for fromMethodDescriptorString; delete after M3.

Throws:
IllegalArgumentException
TypeNotPresentException

Java™ Platform
Standard Ed. 7

DRAFT ea-b118

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, 2010, Oracle Corporation. All rights reserved.
DRAFT ea-b118

Scripting on this page tracks web page traffic, but does not change the content in any way.