Module java.base

Class 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 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 parameters
        endPosition - - 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 class Object
        Parameters:
        x - Object the object to compare with this object.
        Returns:
        boolean true if the object is the same as this object false 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 string
        loader - - 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-formed
        TypeNotPresentException - - 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 class Object
        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 into
        types - - 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 into
        types - - 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 MethodHandle
        parameter0 - - 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 type
        parameters - - the MethodType's parameters
        Returns:
        the interned MethodType
        Throws:
        NullPointerException - - if the return type or parameters are null
        IllegalArgumentException - - if any of the parameters is void
      • methodType

        public static MethodType methodType​(Class<?> type,
                                            Class<?> parameter0,
                                            Class<?>... parameters)
        Wrapper on methodType(Class, Class[]).
        parameter0 is appended to the remaining parameters.
        Parameters:
        type - - the return type
        parameter0 - - the first parameter
        parameters - - the remaining parameters
        Returns:
        a MethodType object
      • methodType

        public static MethodType methodType​(Class<?> returnType,
                                            MethodType methodType)
        Wrapper on methodType(Class, Class[]). Return a MethodType made from the returnType and parameters of the passed in MethodType.
        Parameters:
        returnType - - the return type of the new MethodHandle
        methodType - - 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 on methodType(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
      • 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()

        Overrides:
        toString in class Object
        Returns:
        String a printable representation for the receiver.
      • unwrap

        public MethodType unwrap()
        Wrapper method on methodType(Class, Class[]). Replaces all wrapper types with the appropriate primitive types, including changing Void to void.
        Returns:
        a MethodType without any wrapper types
        See Also:
        wrap()
      • wrap

        public MethodType wrap()
        Wrapper method on methodType(Class, Class[]). Replaces all primitive types with the appropriate wrapper types, including changing void to Void.
        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 arguments
        NullPointerException - - 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 arguments
        NullPointerException - - if the classes is null or contains null