📌  相关文章
📜  Java的.lang.MethodType类在Java中

📅  最后修改于: 2022-05-13 01:54:44.787000             🧑  作者: Mango

Java的.lang.MethodType类在Java中

MethodType 是一个属于Java.lang 包的类。此类由各种类型的方法组成,这些方法在大多数情况下有助于根据输入对象或方法的参数查找方法类型。

  • methodType 的所有实例都是不可变的。这意味着 methodType 类的对象不能被任何其他对象数据覆盖。
  • 在或多或少的情况下,methodType 对象源自字节代码指令。
  • 与所有其他类一样,MethodType 类也可以单独将 CONSTANT_MethodType 表示到常量池。
  • MethodType 类的大多数方法都是使用静态定义的,因为这些方法需要绑定到它们的 Methodtype 类而不是对象。

语法:类声明

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.

}

MethodType 类的方法:

MethodsDescription
appendParameterTypes(Class… ptypesToInsert)This method Finds or creates a method type with additional parameter types.
 appendParameterTypes(List> 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> 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.

此类的示例方法

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();
    }
}





输出
GeeksForGeeks