當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 getGenericParameterTypes()用法及代碼示例


Method類的java.lang.reflect.Method.getGenericParameterTypes()方法返回表示參數類型的Type對象數組,這些對象類型在編碼時在method中聲明。這意味著getGenericParameterTypes()方法返回屬於方法對象的參數數組。如果方法對象不帶參數,則返回長度為0的數組。

如果形式參數類型是參數化類型,則為其返回的Type對象必須準確反映源代碼中使用的實際類型參數。例如,對於方法public void getValue(T value){},將類型參數T替換為參數化類型(即List),然後方法將返回“java.util.List”作為參數類型。

用法:



public Type[] getGenericParameterTypes()

返回值:此方法以聲明順序返回表示方法對象的形式參數類型的Types數組。

異常:此方法引發以下異常:

  • GenericSignatureFormatError-如果通用方法簽名與JVM規範中指定的格式不同。
  • TypeNotPresentException-如果參數類型引用了不存在的類型聲明。
  • MalformedParameterizedTypeException-如果基礎參數類型引用由於某種原因而無法實例化的參數化類型。
    • 以下示例程序旨在說明Method類的getGenericParameterTypes()方法:

      程序1:打印為方法聲明的所有參數類型

      // Program Demonstrate how to apply getGenericParameterTypes() method 
      // of Method Class. 
        
      import java.lang.reflect.Method; 
      import java.lang.reflect.Type; 
        
      public class GFG { 
        
          // Main method 
          public static void main(String[] args) 
          { 
              try { 
                  // create class object 
                  Class classobj = demoClass.class; 
        
                  // get Method Object 
                  Method[] methods = classobj.getMethods(); 
        
                  // iterate through methods 
                  for (Method method:methods) { 
        
                      // only taking method defined in the demo class 
                      if (method.getName().equals("setValue") 
                          || method.getName().equals("getValue") 
                          || method.getName().equals("setManyValues")) { 
        
                          // apply getGenericParameterTypes() method 
                          Type[] parameters = method.getGenericParameterTypes(); 
        
                          // print parameter Types of method Object 
                          System.out.println("\nMethod Name:"
                                             + method.getName()); 
                          System.out.println("No of Parameters:"
                                             + parameters.length); 
                          System.out.println("Parameter object details:"); 
                          for (Type type:parameters) { 
        
                              System.out.println(type.getTypeName()); 
                          } 
                      } 
                  } 
              } 
              catch (Exception e) { 
                  e.printStackTrace(); 
              } 
          } 
      } 
      // a simple class 
      class demoClass { 
        
          // method containing two parameter 
          public void setValue(String value1, String value2) 
          { 
              System.out.println("setValue"); 
          } 
        
          // method containing no parameter 
          public String getValue() 
          { 
              System.out.println("getValue"); 
              return "getValue"; 
          } 
        
          // method containg many parameter 
          public void setManyValues(int value1, 
                                    double value2, 
                                    String value3) 
          { 
              System.out.println("setManyValues"); 
          } 
      }
      輸出:
      Method Name:setManyValues
      No of Parameters:3
      Parameter object details:
      int
      double
      java.lang.String
      
      Method Name:getValue
      No of Parameters:0
      Parameter object details:
      
      Method Name:setValue
      No of Parameters:2
      Parameter object details:
      java.lang.String
      java.lang.String
      

      程序2:檢查Method對象是否包含參數

      // Program Demonstrate how to apply getGenericParameterTypes() 
      // method of Method Class. 
      import java.lang.reflect.Method; 
      import java.lang.reflect.Type; 
        
      public class GFG { 
        
          // Main method 
          public static void main(String[] args) 
          { 
              try { 
                  // create class object 
                  Class classobj = sample.class; 
        
                  Method[] methods = classobj.getMethods(); 
        
                  /*check whether setManyValues() method contains  
                               int parameter or not 
                      and print no of string parameter it contains*/
        
                  for (Method method:methods) { 
                      if (method.getName().equals("setValue")) { 
        
                          int count = containsParameter( 
                              method, 
                              (Type)java.lang.String.class); 
                          System.out.println("No of String"
                                             + " Parameters in setValue():"
                                             + count); 
                      } 
                  } 
        
                  // check whether setManyValues() method 
                  // contains int parameter or not 
                  // and print no of string parameter it contains 
        
                  for (Method method:methods) { 
        
                      if (method.getName().equals("setManyValues")) { 
        
                          int count = containsParameter(method, 
                                                        (Type) int.class); 
        
                          System.out.println("No of int Parameters"
                                             + " in setManyValues():"
                                             + count); 
                      } 
                  } 
              } 
              catch (Exception e) { 
                  e.printStackTrace(); 
              } 
          } 
        
          // count no of parameters contain by 
          // method same as passed to method 
          private static int
          containsParameter(Method method, 
                            Type parameterName) 
          { 
              int count = 0; 
        
              // get all parameter types list 
              // using getGenericParameterTypes() 
              Type parameters[] = method.getGenericParameterTypes(); 
        
              for (int i = 0; i < parameters.length; i++) { 
                  // check contains parameter or not 
                  if (parameters[i] == parameterName) { 
                      count++; 
                  } 
              } 
        
              return count; 
          } 
      } 
      // a simple class 
      class sample { 
        
          // method containing two parameter 
          public void setValue(String value1, 
                               String value2) 
          { 
              System.out.println("setValue"); 
          } 
        
          // method containing many parameter 
          public void setManyValues(int value1, 
                                    double value2, 
                                    String value3) 
          { 
              System.out.println("setManyValues"); 
          } 
      }
      輸出:
      No of String Parameters in setValue():2
      No of int Parameters in setManyValues():1
      

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




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getGenericParameterTypes() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。