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-
相關用法
- Java Class getTypeParameters()用法及代碼示例
- Java Constructor getTypeParameters()用法及代碼示例
- Java Method類 getGenericReturnType()用法及代碼示例
- Java Method類 toGenericString()用法及代碼示例
- Java Method類 isDefault()用法及代碼示例
- Java Method類 isBridge()用法及代碼示例
- Java Method類 getDefaultValue()用法及代碼示例
- Java Method類 getName()用法及代碼示例
- Java Method類 getGenericParameterTypes()用法及代碼示例
- Java Method類 getReturnType()用法及代碼示例
- Java Method類 getParameterAnnotations()用法及代碼示例
- Java Method類 getAnnotatedReturnType()用法及代碼示例
- Java Method類 getParameterTypes()用法及代碼示例
- Java Method類 getModifiers()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getTypeParameters() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。