Constructor getGenericParameterTypes() method in Java with Examples

The getGenericParameterTypes() method of java.lang.reflect.Constructor class is used to return an array of objects that represent the types of the parameters present on this constructor object. The arrangement Order of the returned array from this method is the same as the order of parameter present on this constructor object. If the constructor has no parameters then the method returns an array of length 0.

Syntax:

public Type[] getGenericParameterTypes()

Parameters: This method accepts nothing.



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

Exception: This method throws following exceptions:

  • GenericSignatureFormatError: if the generic method signature does not conform to the format specified in The Java™ Virtual Machine Specification.
  • TypeNotPresentException: if the underlying executable’s throws clause refers to a non-existent type declaration.
  • MalformedParameterizedTypeException: if the underlying executable’s throws clause refers to a parameterized type that cannot be instantiated for any reason.

Below programs illustrate getGenericParameterTypes() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// getGenericParameterTypes() method
  
import java.io.IOException;
import java.lang.reflect.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = shape.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons
            = classObj.getConstructors();
  
        // get array of GenericParameterTypes
        Type[] parameters
            = cons[0].getGenericParameterTypes();
  
        // print length of GenericParameterTypes array
        System.out.println("Parameters : ");
  
        for (int i = 0; i < parameters.length; i++)
            System.out.println(parameters[i]);
    }
  
    // demo class
    public class shape {
  
        public shape(int a, long b, double c)
        {
        }
    }
}

chevron_right


Output:

Parameters : 
class GFG
int
long
double

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// getGenericParameterTypes() method
  
import java.lang.reflect.Constructor;
import java.lang.reflect.Type;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons
            = classObj.getConstructors();
  
        // get array of GenericParameterTypes
        Type[] params
            = cons[0].getGenericParameterTypes();
  
        // print length of GenericParameterTypes array
        System.out.println("No of Parameters: "
                           + params.length);
    }
}

chevron_right


Output:

No of Parameters: 3

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getGenericParameterTypes()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.