java.lang.reflect.Constructor.getGenericParameterTypes() Method Example



Description

The java.lang.reflect.Constructor.getGenericParameterTypes() method returns an array of Type objects that represent the formal parameter types, in declaration order, of the method represented by this Constructor object. Returns an array of length 0 if the underlying method takes no parameters.

Declaration

Following is the declaration for java.lang.reflect.Constructor.getGenericParameterTypes() method.

public Type[] getGenericParameterTypes()

Returns

an array of Types that represent the formal parameter types of the underlying method, in declaration order.

Exceptions

  • GenericSignatureFormatError − if the generic method signature does not conform to the format specified in The Java Virtual Machine Specification.

  • TypeNotPresentException − if any of the parameter types of the underlying method refers to a non-existent type declaration.

  • MalformedParameterizedTypeException − if any of the underlying method's parameter types refer to a parameterized type that cannot be instantiated for any reason.

Example

The following example shows the usage of java.lang.reflect.Constructor.getGenericParameterTypes() method.

Live Demo
package com.tutorialspoint;

import java.lang.reflect.Constructor;
import java.lang.reflect.Type;

public class ConstructorDemo {

   public static void main(String[] args) {

      Constructor[] constructors = SampleClass.class.getConstructors();
      Type[] parameters = constructors[1].getGenericParameterTypes();
      for (int i = 0; i < parameters.length; i++) {
         System.out.println(parameters[i]);
      }
   }
}

class SampleClass {
   private String sampleField;

   public SampleClass() throws ArrayIndexOutOfBoundsException {
   }

   public SampleClass(String sampleField){
      this.sampleField = sampleField;
   }

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField; 
   } 
}

Let us compile and run the above program, this will produce the following result −

class java.lang.String
java_reflect_constructor.htm
Advertisements