📜  方法类 | Java中的 getTypeParameters() 方法

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

方法类 | Java中的 getTypeParameters() 方法

Method 类的Java.lang.reflect.Method.getTypeParameters()方法按声明顺序返回此 Method 对象的泛型声明所声明的 TypeVariable 对象数组。 array 的元素表示 Method 声明的类型变量对象。如果方法对象泛型声明不包含类型变量,则此 getTypeParameters() 将返回长度为 0 的数组。

句法:

public TypeVariable[] getTypeParameters()

返回值:此方法返回由此 Method 对象的泛型声明声明的 TypeVariable 对象数组

例外:如果此方法对象的通用签名与 JVM 规范中指定的格式不匹配,则此方法返回GenericSignatureFormatError

下面的程序说明了 Method 类的 getTypeParameters() 方法:

示例 1:

说明:此代码获取类的所有方法的列表。如果在这些方法的声明时定义了一些 TypeVariable,则这些然后通过循环迭代并获取 TypeVariable。如果这些方法有一些可用的 TypeVariable,则打印 TypeVariable 名称。

Java
/*
* Program Demonstrate getTypeParameters() method 
* of Method Class.
*/
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
  
public class GFG {
  
    // In this method, there is a
    // Type parameter N which extends Number class
    public  void getSampleMethod(N n)
    {
    }
  
    // create main method
    public static void main(String args[])
    {
  
        try {
  
            // create class object for class name GFG
            Class c = GFG.class;
  
            // get list of all Method objects of class GFG
            Method[] methods = c.getMethods();
  
            // loop through all methods and
            // try to get Type Parameter of Method
            for (Method m : methods) {
  
                // get TypeVariable array by getTypeParameters() method
                TypeVariable[] types = m.getTypeParameters();
  
                // print Type Parameter details for every TypeVariable
                for (TypeVariable t : types) {
  
                    // print type parameter name
                    // along with there method name
                    System.out.println("Type variable for Method Name "
                                       + m.getName() + " is "
                                       + t.getName());
                }
            }
        }
        catch (Exception e) {
  
            // print Exception Message if
            // any exception occured in program
            e.printStackTrace();
        }
    }
}


Java
/*
* Program Demonstrate getTypeParameters() method 
* of Method Class having more than one type 
parameter of methods
*/
import java.lang.*;
  
public class GFG {
  
    // In this method,
    // there are three Type parameters
    // N which extends Number class,
    // E extends RuntimeException Class
    // and C extends Character class.
    public  void
    getSampleMethod(N n) throws E
    {
    }
  
    // In this method,
    // there are Two Type parameters :
    // A which extends the ArrayList class,
    // L extends the LinkedList class
    public  L
    SetSampleMethod(A a, L l)
    {
        return l;
    }
  
    // create main method of class
    public static void main(String args[])
    {
  
        try {
  
            // create class object for
            // class name GFG to get methods list
            // of GFG class
            Class c = GFG.class;
  
            // get list of all Method objects of
            // class GFG in array of Methods
            Method[] methods = c.getMethods();
  
            // loop through all methods and
            // try to get Type Parameter of Method
            for (Method m : methods) {
  
                // get TypeVariable array by
                // getTypeParameters method
                TypeVariable[] types = m.getTypeParameters();
  
                // If there are 1 or more than 1
                // type variables for the current
                // method of loop then print method name
                if (types.length > 0)
                    System.out.println("\nType variable Details"
                                       + " for Method Name "
                                       + m.getName());
  
                // print Type Parameter details
                // for Current Method of loop
                for (TypeVariable t : types) {
                    // get bounds for current TypeVariable
                    // and print the Name of TypeVariable and bounds
                    Type[] bounds = t.getBounds();
  
                    // print TypeVariable name and Bounds
                    System.out.println("Name : "
                                       + t.getName());
                    System.out.println("Bounds : "
                                       + Arrays.toString(bounds));
                }
            }
        }
        catch (Exception e) {
            // print Exception message if some Exception occurs
            e.printStackTrace();
        }
    }
}


示例 2:在这个程序中,方法的类型参数不止一种。在这个程序中,使用 getTypeParameter()函数获取类型参数并打印这些类型参数的详细信息。

Java

/*
* Program Demonstrate getTypeParameters() method 
* of Method Class having more than one type 
parameter of methods
*/
import java.lang.*;
  
public class GFG {
  
    // In this method,
    // there are three Type parameters
    // N which extends Number class,
    // E extends RuntimeException Class
    // and C extends Character class.
    public  void
    getSampleMethod(N n) throws E
    {
    }
  
    // In this method,
    // there are Two Type parameters :
    // A which extends the ArrayList class,
    // L extends the LinkedList class
    public  L
    SetSampleMethod(A a, L l)
    {
        return l;
    }
  
    // create main method of class
    public static void main(String args[])
    {
  
        try {
  
            // create class object for
            // class name GFG to get methods list
            // of GFG class
            Class c = GFG.class;
  
            // get list of all Method objects of
            // class GFG in array of Methods
            Method[] methods = c.getMethods();
  
            // loop through all methods and
            // try to get Type Parameter of Method
            for (Method m : methods) {
  
                // get TypeVariable array by
                // getTypeParameters method
                TypeVariable[] types = m.getTypeParameters();
  
                // If there are 1 or more than 1
                // type variables for the current
                // method of loop then print method name
                if (types.length > 0)
                    System.out.println("\nType variable Details"
                                       + " for Method Name "
                                       + m.getName());
  
                // print Type Parameter details
                // for Current Method of loop
                for (TypeVariable t : types) {
                    // get bounds for current TypeVariable
                    // and print the Name of TypeVariable and bounds
                    Type[] bounds = t.getBounds();
  
                    // print TypeVariable name and Bounds
                    System.out.println("Name : "
                                       + t.getName());
                    System.out.println("Bounds : "
                                       + Arrays.toString(bounds));
                }
            }
        }
        catch (Exception e) {
            // print Exception message if some Exception occurs
            e.printStackTrace();
        }
    }
}

参考:
https://docs.oracle.com/javase/8/docs/api/java Java