Open In App

java.lang.MethodType Class in Java

Last Updated : 08 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • All the instances of methodType are immutable. This means the objects of the methodType class cannot be overridden by any other objects data.
  • In more or less situations the methodType Objects are derived from Byte code instructions.
  • Like all other classes, the MethodType class can also individually represent CONSTANT_MethodType to the pool of constants.
  • Most of the methods of class MethodType are defined using Static cause these methods need to bound to their Methodtype class rather than to the Objects.

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 method finds 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<Class<?>> 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




// Java Program to demonstrate MethodType Class
// from java.lang.invoke package
 
// Importing required classes from java.lang package
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
 
// Main class
class GFG {
 
    // Method 1
    // Helper method
    static void hello()
    {
 
        // Print statement
        System.out.println("GeeksForGeeks");
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args) throws Throwable
    {
 
        // Creating an object MethodHandles class to
        // create an object lookup
        MethodHandles.Lookup lookup
            = MethodHandles.lookup();
 
        // Returning the type of method used using
        // methodType class
 
        // Now, we are also invoking the hello() method to
        // print the context present in it
        MethodHandle mh = lookup.findStatic(
            GFG.class, "hello",
            MethodType.methodType(void.class));
 
        // Lastly invoking invokeExact() method
        // using method handle
        mh.invokeExact();
    }
}


 
 

Output

GeeksForGeeks

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads