Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaMethod Class | getGenericParameterTypes() method in Java

Method Class | getGenericParameterTypes() method in Java

The java.lang.reflect.Method.getGenericParameterTypes() method of Method class returns an array of Type objects that represent the parameter types, declared in method at time of coding. It means that the getGenericParameterTypes() method returns array of parameters that belongs to method object. It returns an array of length 0 if the method object takes no parameters.

If a formal parameter type is a parameterized type, the Type object returned for it must accurately reflect the actual type parameters used in the source code. e.g for method public void getValue(T value){} substitute a type parameter T with a parameterized type (i.e., List) then method will return “java.util.List” as parameter type.

Syntax:

public Type[] getGenericParameterTypes()

Return Value: This method returns an array of Types that represent the formal parameter types of the method object, in declaration order.

Exception: This method throws following exceptions:

  • GenericSignatureFormatError – if the generic method signature is not same as the format specified in The JVM Specification.
  • TypeNotPresentException – if parameter types refers to a non-existent type declaration.
  • MalformedParameterizedTypeException – if the underlying parameter types refers to a parameterized type that cannot be instantiated for any reason.
    • Below programs illustrates getGenericParameterTypes() method of Method class:

      Program 1: Print all Parameter type declared for 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 = 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 containing many parameter
          public void setManyValues(int value1,
                                    double value2,
                                    String value3)
          {
              System.out.println("setManyValues");
          }
      }

      
      
      Output:

      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
      

      Program 2: Check if the Method object contains parameter or not




      // 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");
          }
      }

      
      
      Output:

      No of String Parameters in setValue(): 2
      No of int Parameters in setManyValues(): 1
      

      Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getGenericParameterTypes–

RELATED ARTICLES

Most Popular

Recent Comments