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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。