Method Class | getTypeParameters() Method in Java
Last Updated :
13 Jun, 2022
The java.lang.reflect.Method.getTypeParameters() method of Method class returns an array of TypeVariable objects declared by the generic declaration of this Method object, in declaration order. Elements of array represent the type variables objects declared by Method. An array of length 0 is returned by this getTypeParameters(), if the Method Object generic declaration contains no type variables.
Syntax:
public TypeVariable<Method>[] getTypeParameters()
Return Value: This method returns an array of TypeVariable objects declared by the generic declaration of this Method object
Exception: This method returns GenericSignatureFormatError if the generic signature of this Method object does not match to the format specified in The JVM Specification.
Below program illustrates getTypeParameters() method of Method class:
Example 1: Explanation: This code fetches list of all Method of a class. These are then iterated through loop and the TypeVariable is fetched, if there are some TypeVariable defined at time of Declaration of those methods. If there are some TypeVariable available for those methods, then TypeVariable name is printed.
Java
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
public class GFG {
public <N extends Number> void getSampleMethod(N n)
{
}
public static void main(String args[])
{
try {
Class c = GFG. class ;
Method[] methods = c.getMethods();
for (Method m : methods) {
TypeVariable[] types = m.getTypeParameters();
for (TypeVariable t : types) {
System.out.println( "Type variable for Method Name "
+ m.getName() + " is "
+ t.getName());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
Type variable for Method Name getSampleMethod is N
Example 2: In this program there are more than one type parameter of methods. In this program, the type parameter are fetched using getTypeParameter() function and prints details of those type parameters.
Java
import java.lang.*;
public class GFG {
public <N extends Number,
E extends RuntimeException,
C extends Character> void
getSampleMethod(N n) throws E
{
}
public <A extends ArrayList, L extends LinkedList> L
SetSampleMethod(A a, L l)
{
return l;
}
public static void main(String args[])
{
try {
Class c = GFG. class ;
Method[] methods = c.getMethods();
for (Method m : methods) {
TypeVariable[] types = m.getTypeParameters();
if (types.length > 0 )
System.out.println( "\nType variable Details"
+ " for Method Name "
+ m.getName());
for (TypeVariable t : types) {
Type[] bounds = t.getBounds();
System.out.println( "Name : "
+ t.getName());
System.out.println( "Bounds : "
+ Arrays.toString(bounds));
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
Type variable Details for Method Name getSampleMethod
Name : N
Bounds : [class java.lang.Number]
Name : E
Bounds : [class java.lang.RuntimeException]
Name : C
Bounds : [class java.lang.Character]
Type variable Details for Method Name SetSampleMethod
Name : A
Bounds : [class java.util.ArrayList]
Name : L
Bounds : [class java.util.LinkedList]
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getTypeParameters–
Share your thoughts in the comments
Please Login to comment...