public final class MethodType extends Object implements Serializable
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.
Modifier and Type | Method and 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 on
methodType(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.
|
static MethodType |
methodType(Class<?> type)
Create a MethodType object with the specified return type and no parameters
|
static 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 on
methodType(Class, Class[]) . |
static MethodType |
methodType(Class<?> type,
List<Class<?>> parameters)
Wrapper on
methodType(Class, Class[]) |
static MethodType |
methodType(Class<?> returnType,
MethodType methodType)
Wrapper on
methodType(Class, Class[]) . |
Class<?>[] |
parameterArray()
Helper method to return the parameter types in an array.
|
int |
parameterCount()
Helper method to return the number of parameters
|
List<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 on
methodType(Class, Class[]) . |
MethodType |
wrap()
Wrapper method on
methodType(Class, Class[]) . |
public MethodType changeParameterType(int position, Class<?> type)
position
- - the position, starting from zero, of the parameter to be changed.type
- - the Class to change the parameter topublic MethodType changeReturnType(Class<?> type)
type
- - the Class that the return type should be changed to.public MethodType dropParameterTypes(int startPosition, int endPosition) throws IndexOutOfBoundsException
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.IndexOutOfBoundsException
- if the startPosition or endPosition are not valid indexes or if the startPosition is greater than the endPositionpublic boolean equals(Object x)
equals
in class Object
x
- Object
the object to compare with this object.true
if the object is the same as this object
false
if it is different from this object.Object.hashCode()
public MethodType erase()
public static MethodType fromMethodDescriptorString(String methodDescriptor, ClassLoader loader)
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
methodDescriptor
- - the method descriptor stringloader
- - the ClassLoader to be used or null for System ClassLoaderIllegalArgumentException
- - if the string is not well-formedTypeNotPresentException
- - if a named type cannot be foundpublic MethodType generic()
public int hashCode()
hashCode
in class Object
Object.equals(java.lang.Object)
public boolean hasPrimitives()
public boolean hasWrappers()
java.lang.Void is only treated as a wrapper if it occurs as the class of the return.
public MethodType insertParameterTypes(int position, Class<?>... types) throws IndexOutOfBoundsException
position
- - the position to insert intotypes
- - zero or mores types for the new parametersIndexOutOfBoundsException
- if position is less than 0 or greater than the number of argumentspublic MethodType insertParameterTypes(int position, List<Class<?>> types)
position
- - the position to insert intotypes
- - zero or mores types for the new parametersIllegalArgumentException
- if position is less than 0 or greater than the number of argumentspublic static MethodType methodType(Class<?> type)
type
- - the return type of the MethodHandlepublic static MethodType methodType(Class<?> type, Class<?> parameter0)
type
- - the return type of the MethodHandleparameter0
- - the type of the single parameterpublic static MethodType methodType(Class<?> returnType, Class<?>[] parameters)
returnType
- - the MethodType's return typeparameters
- - the MethodType's parametersNullPointerException
- - if the return type or parameters are nullIllegalArgumentException
- - if any of the parameters is voidpublic static MethodType methodType(Class<?> type, Class<?> parameter0, Class<?>... parameters)
methodType(Class, Class[])
.
type
- - the return typeparameter0
- - the first parameterparameters
- - the remaining parameterspublic static MethodType methodType(Class<?> type, List<Class<?>> parameters)
methodType(Class, Class[])
type
- - the return typeparameters
- - the parameter typespublic static MethodType methodType(Class<?> returnType, MethodType methodType)
methodType(Class, Class[])
.
Return a MethodType made from the returnType and parameters of the passed in MethodType.returnType
- - the return type of the new MethodHandlemethodType
- - the MethodType to take the parameter types frompublic static MethodType genericMethodType(int numParameters) throws IllegalArgumentException
numParameters
- - number of parametersIllegalArgumentException
- if numParameters is less than 0 or greater than the allowed number of argumentspublic static MethodType genericMethodType(int numParameters, boolean isVarargs) throws IllegalArgumentException
methodType(Class, Class[])
.
numParameters
- - number of parameters not including the isVarargs parameter (if requested)isVarargs
- - if the Object[] parameter should be addedIllegalArgumentException
- if numParameters is less than 0 or greater than the allowed number of arguments (255 or 254 if isVarargs)public Class<?>[] parameterArray()
public int parameterCount()
public List<Class<?>> parameterList()
public Class<?> parameterType(int position) throws IndexOutOfBoundsException
position
- - the parameter to get the type ofIndexOutOfBoundsException
- if position is less than 0 or an invalid argument index.public Class<?> returnType()
public String toMethodDescriptorString()
fromMethodDescriptorString(String, ClassLoader)
public String toString()
Note that this is not the same as toMethodDescriptorString()
public MethodType unwrap()
methodType(Class, Class[])
. Replaces all wrapper types with
the appropriate primitive types, including changing Void
to
void.wrap()
public MethodType wrap()
methodType(Class, Class[])
. Replaces all primitive types with
the appropriate wrapper types, including changing void to Void
.unwrap()
public MethodType appendParameterTypes(Class<?>... classes) throws IllegalArgumentException, NullPointerException
classes
- - the new parameter types to add to the end of the MethodType's argument typesIllegalArgumentException
- - 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 nullpublic MethodType appendParameterTypes(List<Class<?>> classes) throws IllegalArgumentException, NullPointerException
classes
- - the new parameter types to add to the end of the MethodType's argument typesIllegalArgumentException
- - 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 nullEclipse OpenJ9 website.
To raise a bug report or suggest an improvement create an Eclipse OpenJ9 issue.
Copyright © 1993, 2023 IBM Corp. and others.