当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Method类 getTypeParameters()用法及代码示例


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

用法:

public TypeVariable<Method>[] getTypeParameters()

返回值:此方法返回此Method对象的通用声明所声明的TypeVariable对象的数组



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

以下示例程序旨在说明Method类的getTypeParameters()方法:

范例1:

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

/* 
* 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 <N extends Number> 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 Messgae if 
            // any exception occured in program 
            e.printStackTrace(); 
        } 
    } 
}
输出:
Type variable for Method Name getSampleMethod is N

范例2:在此程序中,有多个类型的方法参数。在此程序中,使用getTypeParameter()函数获取类型参数,并打印这些类型参数的详细信息。

/* 
* 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 <N extends Number, 
                      E extends RuntimeException, 
                                C extends Character> 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 <A extends ArrayList, L extends LinkedList> 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(); 
        } 
    } 
}
输出:
Type variable Details for Method Name getSampleMethod
Name:N
Bounds:[class java.lang.Number]
Name:E
Bounds:[class java.lang.RuntimeException]
Name:C
Bounds:[class java.lang.Character]

Type variable Details for Method Name SetSampleMethod
Name:A
Bounds:[class java.util.ArrayList]
Name:L
Bounds:[class java.util.LinkedList]

参考:
https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getTypeParameters-




相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Method Class | getTypeParameters() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。