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


Advertisements

Description

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

Declaration

Following is the declaration for java.lang.reflect.Method.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.Method.getGenericParameterTypes() method.

Live Demo
package com.tutorialspoint;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class MethodDemo {

   public static void main(String[] args) {

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

class SampleClass {
   private String 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_method.htm
Advertisements