java.lang.MethodType Class in Java (original) (raw)

Last Updated : 08 Jul, 2021

MethodType is a Class that belongs to java.lang package. This class consists of various types of methods that help in most of cases to find the method type based on the input object, or parameters of the method.

Syntax: Class declaration

public final class MethodType extends Object implements Serializable {

// MethodType extends Object Class which is root of class hierarchy // The serialization interface has no methods or fields and serves only // to identify the semantics of being serializable.

}

Methods of MethodType class:

Methods Description
appendParameterTypes(Class<?>… ptypesToInsert) This method Finds or creates a method type with additional parameter types.
appendParameterTypes(List<Class<?>> ptypesToInsert) This method Finds or creates a method type with additional parameter types.
changeParameterType() This method finds or creates a method type with a single different parameter type.
changeReturnType() This method finds or creates a method type with a different return type.
dropParameterTypes() This method finds or creates a method type with some parameter types omitted.
equals() This method Compares the specified object with this type for equality.
erase() This method Erases all reference types to Object.
fromMethodDescriptorString() This method finds or creates an instance of a method type, given the spelling of its bytecode descriptor.
generic() This method Converts all types, both reference and primitive, to Object.
genericMethodType(int objectArgCount) This method finds or creates a method type whose components are all Object.
genericMethodType(int objectArgCount, boolean finalArray) This methodfinds or creates a method type whose components are Object with an optional trailing Object[] array.
hashCode() This method returns the hash code value for this method type.
hasPrimitives() This method reports if this type contains a primitive argument or return value.
hasWrappers() This method reports if this type contains a wrapper argument or return value.
insertParameterTypes(int num, Class<) This method Finds or creates a method type with additional parameter types.
insertParameterTypes(int num, List<Class<?>> ptypesToInsert) This method Finds or creates a method type with additional parameter types.
methodType(Class<?> rtype) This method Finds or creates a method type with the given components.
methodType(Class rtype, Class ptype0) This method Finds or creates a method type with the given components.
methodType(Class rtype, Class[] ptypes) This method Finds or creates an instance of the given method type.
methodType(Class rtype, Class ptype0, Class<?>… ptypes) This method Finds or creates a method type with the given components.
methodType(Class rtype, List> ptypes) This method Finds or creates a method type with the given components.
methodType(Class<?> rtype, MethodType ptypes) This method Finds or creates a method type with the given components.
parameterArray() This method Presents the parameter types as an array (a convenience method).
parameterCount() This method returns the number of parameter types in this method type.
parameterList() This method Presents the parameter types as a list (a convenience method).
parameterType(int num) This method returns the parameter type at the specified index, within this method type.
returnType() This method returns the return type of this method type.
toMethodDescriptorString() This method Produces a bytecode descriptor representation of the method type.
toString() This method returns a string representation of the method type, of the form “(PT0,PT1…)RT”.
unwrap() This method Converts all wrapper types to their corresponding primitive types.
wrap() This method Converts all primitive types to their corresponding wrapper types.

Example Methods of this class

Java

import java.lang.invoke.MethodHandle;

import java.lang.invoke.MethodHandles;

import java.lang.invoke.MethodType;

class GFG {

`` static void hello()

`` {

`` System.out.println( "GeeksForGeeks" );

`` }

`` public static void main(String[] args) throws Throwable

`` {

`` MethodHandles.Lookup lookup

`` = MethodHandles.lookup();

`` MethodHandle mh = lookup.findStatic(

`` GFG. class , "hello" ,

`` MethodType.methodType( void . class ));

`` mh.invokeExact();

`` }

}

Similar Reads