Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaMethod Class | getTypeParameters() Method in Java

Method Class | getTypeParameters() Method in Java

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




/*
* Program Demonstrate getTypeParameters() method
* of Method Class.
*/
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
 
public class GFG {
 
    // In this method, there is a
    // Type parameter N which extends Number class
    public <N extends Number> void getSampleMethod(N n)
    {
    }
 
    // create main method
    public static void main(String args[])
    {
 
        try {
 
            // create class object for class name GFG
            Class c = GFG.class;
 
            // get list of all Method objects of class GFG
            Method[] methods = c.getMethods();
 
            // loop through all methods and
            // try to get Type Parameter of Method
            for (Method m : methods) {
 
                // get TypeVariable array by getTypeParameters() method
                TypeVariable[] types = m.getTypeParameters();
 
                // print Type Parameter details for every TypeVariable
                for (TypeVariable t : types) {
 
                    // print type parameter name
                    // along with there method name
                    System.out.println("Type variable for Method Name "
                                       + m.getName() + " is "
                                       + t.getName());
                }
            }
        }
        catch (Exception e) {
 
            // print Exception Message if
            // any exception occurred in program
            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




/*
* Program Demonstrate getTypeParameters() method
* of Method Class having more than one type
parameter of methods
*/
import java.lang.*;
 
public class GFG {
 
    // In this method,
    // there are three Type parameters
    // N which extends Number class,
    // E extends RuntimeException Class
    // and C extends Character class.
    public <N extends Number,
                      E extends RuntimeException,
                                C extends Character> void
    getSampleMethod(N n) throws E
    {
    }
 
    // In this method,
    // there are Two Type parameters :
    // A which extends the ArrayList class,
    // L extends the LinkedList class
    public <A extends ArrayList, L extends LinkedList> L
    SetSampleMethod(A a, L l)
    {
        return l;
    }
 
    // create main method of class
    public static void main(String args[])
    {
 
        try {
 
            // create class object for
            // class name GFG to get methods list
            // of GFG class
            Class c = GFG.class;
 
            // get list of all Method objects of
            // class GFG in array of Methods
            Method[] methods = c.getMethods();
 
            // loop through all methods and
            // try to get Type Parameter of Method
            for (Method m : methods) {
 
                // get TypeVariable array by
                // getTypeParameters method
                TypeVariable[] types = m.getTypeParameters();
 
                // If there are 1 or more than 1
                // type variables for the current
                // method of loop then print method name
                if (types.length > 0)
                    System.out.println("\nType variable Details"
                                       + " for Method Name "
                                       + m.getName());
 
                // print Type Parameter details
                // for Current Method of loop
                for (TypeVariable t : types) {
                    // get bounds for current TypeVariable
                    // and print the Name of TypeVariable and bounds
                    Type[] bounds = t.getBounds();
 
                    // print TypeVariable name and Bounds
                    System.out.println("Name : "
                                       + t.getName());
                    System.out.println("Bounds : "
                                       + Arrays.toString(bounds));
                }
            }
        }
        catch (Exception e) {
            // print Exception message if some Exception occurs
            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–

RELATED ARTICLES

Most Popular

Recent Comments