- java.lang.Object
-
- java.lang.invoke.MethodType
-
- All Implemented Interfaces:
Serializable
public final class MethodType extends Object implements Serializable
MethodTypes are immutable objects used to correlate MethodHandles with their invocation sites.A MethodType is a composed of the return type and the parameter types. These are represented using Class objects of the corresponding type, be it primitive, void, or reference.
MethodTypes are interned and immutable. As such they can be compared for instance equality (==). Two MethodTypes are equal iff they have the same classes for the their return and parameter types.
- Since:
- 1.7
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MethodType
appendParameterTypes(Class<?>... classes)
Returns a MethodType with the additional class types appended to the end.MethodType
appendParameterTypes(List<Class<?>> classes)
Returns a MethodType with the additional class types appended to the end.MethodType
changeParameterType(int position, Class<?> type)
Convenience method to create a new MethodType with only the parameter at position changed to the new type.MethodType
changeReturnType(Class<?> type)
Convenience method to create a new MethodType with a changed return type.MethodType
dropParameterTypes(int startPosition, int endPosition)
Convenience method to create a new MethodType after dropping the parameters between startPosition and endPosition.boolean
equals(Object x)
Compares the specified object with this type for equality.MethodType
erase()
Convenience method erase all reference types to Object.static MethodType
fromMethodDescriptorString(String methodDescriptor, ClassLoader loader)
Convenience Method to create a MethodType from bytecode-level method descriptor.MethodType
generic()
Convenience method to convert all types to Object.static MethodType
genericMethodType(int numParameters)
Static helper method to create a MethodType with only Object return type and parameters.static MethodType
genericMethodType(int numParameters, boolean isVarargs)
Wrapper onmethodType(Class, Class[])
.int
hashCode()
Returns the MethodType's hash code, which is defined to be the same as the hash code of a List composed of the return type followed by the parameter types.boolean
hasPrimitives()
Helper method to determine if the return type or any of the parameter types are primitives.boolean
hasWrappers()
Helper method to determine if the return type or any of the parameter types are wrappers.MethodType
insertParameterTypes(int position, Class<?>... types)
Return a new MethodType with an additional parameters inserted at position, which is a zero based index.MethodType
insertParameterTypes(int position, List<Class<?>> types)
Return a new MethodType with an additional parameters inserted at position, which is a zero based index.Class<?>
lastParameterType()
Return the last class in the parameterType array.static MethodType
methodType(Class<?> type)
Create a MethodType object with the specified return type and no parametersstatic MethodType
methodType(Class<?> type, Class<?> parameter0)
Return a MethodType object with the specified return type and a single parameter of type 'parameter0'.static MethodType
methodType(Class<?> returnType, Class<?>[] parameters)
Return a MethodType object with the parameter and return types as requested.static MethodType
methodType(Class<?> type, Class<?> parameter0, Class<?>... parameters)
Wrapper onmethodType(Class, Class[])
.static MethodType
methodType(Class<?> returnType, MethodType methodType)
Wrapper onmethodType(Class, Class[])
.static MethodType
methodType(Class<?> type, List<Class<?>> parameters)
Wrapper onmethodType(Class, Class[])
Class<?>[]
parameterArray()
Helper method to return the parameter types in an array.int
parameterCount()
Helper method to return the number of parametersList<Class<?>>
parameterList()
Helper method to return the parameter types in a List.Class<?>
parameterType(int position)
Return the type of the parameter at position.Class<?>
returnType()
String
toMethodDescriptorString()
Create a method descriptor string for this MethodType.String
toString()
Return a string representation of the MethodType in the form: '(A0,A2,A3...)R'.MethodType
unwrap()
Wrapper method onmethodType(Class, Class[])
.MethodType
wrap()
Wrapper method onmethodType(Class, Class[])
.
-
-
-
Method Detail
-
changeParameterType
public MethodType changeParameterType(int position, Class<?> type)
Convenience method to create a new MethodType with only the parameter at position changed to the new type.- Parameters:
position
- - the position, starting from zero, of the parameter to be changed.type
- - the Class to change the parameter to- Returns:
- a new MethodType with the parameter at position changed
-
changeReturnType
public MethodType changeReturnType(Class<?> type)
Convenience method to create a new MethodType with a changed return type.- Parameters:
type
- - the Class that the return type should be changed to.- Returns:
- a new MethodType with a changed return type
-
dropParameterTypes
public MethodType dropParameterTypes(int startPosition, int endPosition) throws IndexOutOfBoundsException
Convenience method to create a new MethodType after dropping the parameters between startPosition and endPosition.- Parameters:
startPosition
- - the position, starting from zero, from which to start dropping parametersendPosition
- - the position of the first parameter not to drop. Must be greater than startPosition.- Returns:
- a new MethodType with parameters between start and end-1 position (inclusive) removed
- Throws:
IndexOutOfBoundsException
- if the startPosition or endPosition are not valid indexes or if the startPosition is greater than the endPosition
-
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 the object to compare with this object.- Returns:
- boolean
true
if the object is the same as this objectfalse
if it is different from this object. - See Also:
Object.hashCode()
-
erase
public MethodType erase()
Convenience method erase all reference types to Object. Primitive and void remain unchanged.- Returns:
- a new MethodType with all non-primitive or void return type and parameters changed to Object.
-
fromMethodDescriptorString
public static MethodType fromMethodDescriptorString(String methodDescriptor, ClassLoader loader)
Convenience Method to create a MethodType from bytecode-level method descriptor. (See JVM Spec 2nd Ed. section 4.4.3).All of the classes used in the method descriptor string must be reachable from a common ClassLoader or an exception will result.
The ClassLoader parameter may be null, in which case the System ClassLoader will be used.
Note, the Class names must use JVM syntax in the method descriptor String and therefore java.lang.Class will be represented as Ljava/lang/Class;
Example method descriptors
- (II)V - method taking two ints and return void
- (I)Ljava/lang/Integer; - method taking an int and returning an Integer
- ([I)I - method taking an array of ints and returning an int
- Parameters:
methodDescriptor
- - the method descriptor stringloader
- - the ClassLoader to be used or null for System ClassLoader- Returns:
- a MethodType object representing the method descriptor string
- Throws:
IllegalArgumentException
- - if the string is not well-formedTypeNotPresentException
- - if a named type cannot be found
-
generic
public MethodType generic()
Convenience method to convert all types to Object.- Returns:
- a new MethodType with both return and parameter types changed to Object
-
hashCode
public int hashCode()
Returns the MethodType's hash code, which is defined to be the same as the hash code of a List composed of the return type followed by the parameter types.- Overrides:
hashCode
in classObject
- Returns:
- the MethodType's hash code
- See Also:
Object.equals(java.lang.Object)
-
hasPrimitives
public boolean hasPrimitives()
Helper method to determine if the return type or any of the parameter types are primitives.- Returns:
- whether the MethodType contains any primitive types
-
hasWrappers
public boolean hasWrappers()
Helper method to determine if the return type or any of the parameter types are wrappers. Wrappers are the boxed versions of primitives.java.lang.Void is only treated as a wrapper if it occurs as the class of the return.
- Returns:
- whether the MethodType contains any wrapper types
-
insertParameterTypes
public MethodType insertParameterTypes(int position, Class<?>... types) throws IndexOutOfBoundsException
Return a new MethodType with an additional parameters inserted at position, which is a zero based index.- Parameters:
position
- - the position to insert intotypes
- - zero or mores types for the new parameters- Returns:
- a new MethodType with the additional parameters at position
- Throws:
IndexOutOfBoundsException
- if position is less than 0 or greater than the number of arguments
-
insertParameterTypes
public MethodType insertParameterTypes(int position, List<Class<?>> types)
Return a new MethodType with an additional parameters inserted at position, which is a zero based index.- Parameters:
position
- - the position to insert intotypes
- - zero or mores types for the new parameters- Returns:
- a new MethodType with the additional parameters at position
- Throws:
IllegalArgumentException
- if position is less than 0 or greater than the number of arguments
-
methodType
public static MethodType methodType(Class<?> type)
Create a MethodType object with the specified return type and no parameters- Parameters:
type
- - the return type of the MethodHandle- Returns:
- a new MethodHandle object
-
methodType
public static MethodType methodType(Class<?> type, Class<?> parameter0)
Return a MethodType object with the specified return type and a single parameter of type 'parameter0'.- Parameters:
type
- - the return type of the MethodHandleparameter0
- - the type of the single parameter- Returns:
- a new MethodHandle object
-
methodType
public static MethodType methodType(Class<?> returnType, Class<?>[] parameters)
Return a MethodType object with the parameter and return types as requested.- Parameters:
returnType
- - the MethodType's return typeparameters
- - the MethodType's parameters- Returns:
- the interned MethodType
- Throws:
NullPointerException
- - if the return type or parameters are nullIllegalArgumentException
- - if any of the parameters is void
-
methodType
public static MethodType methodType(Class<?> type, Class<?> parameter0, Class<?>... parameters)
Wrapper onmethodType(Class, Class[])
.
parameter0 is appended to the remaining parameters.- Parameters:
type
- - the return typeparameter0
- - the first parameterparameters
- - the remaining parameters- Returns:
- a MethodType object
-
methodType
public static MethodType methodType(Class<?> type, List<Class<?>> parameters)
Wrapper onmethodType(Class, Class[])
- Parameters:
type
- - the return typeparameters
- - the parameter types- Returns:
- a MethodType object
-
methodType
public static MethodType methodType(Class<?> returnType, MethodType methodType)
Wrapper onmethodType(Class, Class[])
. Return a MethodType made from the returnType and parameters of the passed in MethodType.- Parameters:
returnType
- - the return type of the new MethodHandlemethodType
- - the MethodType to take the parameter types from- Returns:
- a MethodType object made by changing the return type of the passed in MethodType
-
genericMethodType
public static MethodType genericMethodType(int numParameters) throws IllegalArgumentException
Static helper method to create a MethodType with only Object return type and parameters.- Parameters:
numParameters
- - number of parameters- Returns:
- a MethodType using only Object for return and parameter types.
- Throws:
IllegalArgumentException
- if numParameters is less than 0 or greater than the allowed number of arguments
-
genericMethodType
public static MethodType genericMethodType(int numParameters, boolean isVarargs) throws IllegalArgumentException
Wrapper onmethodType(Class, Class[])
.
Return a MethodType with an Object return and only Object parameters. If isVarargs is true, the final parameter will be an Object[]. This Object[] parameter is NOT included in the numParameters count.- Parameters:
numParameters
- - number of parameters not including the isVarargs parameter (if requested)isVarargs
- - if the Object[] parameter should be added- Returns:
- the requested MethodType object
- Throws:
IllegalArgumentException
- if numParameters is less than 0 or greater than the allowed number of arguments (255 or 254 if isVarargs)
-
parameterArray
public Class<?>[] parameterArray()
Helper method to return the parameter types in an array.
Changes to the array do not affect the MethodType object.- Returns:
- the parameter types as an array
-
parameterCount
public int parameterCount()
Helper method to return the number of parameters- Returns:
- the number of parameters
-
parameterList
public List<Class<?>> parameterList()
Helper method to return the parameter types in a List.
Changes to the List do not affect the MethodType object.- Returns:
- the parameter types as a List
-
parameterType
public Class<?> parameterType(int position) throws IndexOutOfBoundsException
Return the type of the parameter at position.- Parameters:
position
- - the parameter to get the type of- Returns:
- the parameter's type
- Throws:
IndexOutOfBoundsException
- if position is less than 0 or an invalid argument index.
-
returnType
public Class<?> returnType()
- Returns:
- the type of the return
-
lastParameterType
public Class<?> lastParameterType()
Return the last class in the parameterType array. This is equivalent to: type.parameterType(type.parameterCount() - 1)- Returns:
- class of final parameter, or void.class if there are no parameters
-
toMethodDescriptorString
public String toMethodDescriptorString()
Create a method descriptor string for this MethodType.- Returns:
- a method descriptor string
- See Also:
fromMethodDescriptorString(String, ClassLoader)
-
toString
public String toString()
Return a string representation of the MethodType in the form: '(A0,A2,A3...)R'. The simple name of each class is used.Note that this is not the same as
toMethodDescriptorString()
-
unwrap
public MethodType unwrap()
Wrapper method onmethodType(Class, Class[])
. Replaces all wrapper types with the appropriate primitive types, including changingVoid
to void.- Returns:
- a MethodType without any wrapper types
- See Also:
wrap()
-
wrap
public MethodType wrap()
Wrapper method onmethodType(Class, Class[])
. Replaces all primitive types with the appropriate wrapper types, including changing void toVoid
.- Returns:
- a MethodType without any primitive types
- See Also:
unwrap()
-
appendParameterTypes
public MethodType appendParameterTypes(Class<?>... classes) throws IllegalArgumentException, NullPointerException
Returns a MethodType with the additional class types appended to the end.- Parameters:
classes
- - the new parameter types to add to the end of the MethodType's argument types- Returns:
- a MethodType with the additional argument types
- Throws:
IllegalArgumentException
- - if void.class is one of the classes or if the resulting MethodType would have more then 255 argumentsNullPointerException
- - if the classes array is null or contains null
-
appendParameterTypes
public MethodType appendParameterTypes(List<Class<?>> classes) throws IllegalArgumentException, NullPointerException
Returns a MethodType with the additional class types appended to the end.- Parameters:
classes
- - the new parameter types to add to the end of the MethodType's argument types- Returns:
- a MethodType with the additional argument types
- Throws:
IllegalArgumentException
- - if void.class is one of the classes or if the resulting MethodType would have more then 255 argumentsNullPointerException
- - if the classes is null or contains null
-
-