Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Method Class | getParameterTypes() Method in Java
Next article icon

Method Class | getTypeParameters() Method in Java

Last Updated : 13 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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--


Next Article
Method Class | getParameterTypes() Method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • java-basics
  • Java-lang package
  • Java-Functions
  • Java-Method Class
  • java-lang-reflect-package
Practice Tags :
  • Java

Similar Reads

  • Method Class | getParameterTypes() Method in Java
    Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2 java.lang.reflectMethod class help us to get information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. getParameterTypes() m
    4 min read
  • Method Class | getParameterCount() method in Java
    The java.lang.reflect.Method.getParameterCount() method of Method class returns the number of parameters declared on a method Object. Syntax: public int getParameterCount() Return Value: This method returns number of formal parameters defined on this Method Object. Below programs illustrates getPara
    3 min read
  • Method Class | getGenericParameterTypes() method in Java
    The java.lang.reflect.Method.getGenericParameterTypes() method of Method class returns an array of Type objects that represent the parameter types, declared in method at time of coding. It means that the getGenericParameterTypes() method returns array of parameters that belongs to method object. It
    4 min read
  • Class getTypeParameters() method in Java with Examples
    The getTypeParameters() method of java.lang.Class class is used to get the type parameters of this entity. This entity can be a class, an array, an interface, etc. The method returns an array of TypeVariable objects representing the type variables.Syntax: public TypeVariable<Class<T>> ge
    2 min read
  • Method Class | getParameterAnnotations() method in Java
    The java.lang.reflect.Method.getParameterAnnotations() method of Method class returns a two-dimensional Annotation array, that represents the annotations on the parameters, of the Method object. If the Method contains no parameters, an empty array will be returned. If the Method contains one or more
    4 min read
  • Method Class | getName() Method in Java
    The getName() method of java.lang.reflect.Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Syntax: public String getName() Return Value: It returns the name o
    3 min read
  • Method Class | getModifiers() method in Java
    The java.lang.reflect.Method.getModifiers() method of Method class returns the modifiers for the method represented by this Method object. It returns int value. Then with the use Modifier class, the name of modifiers corresponding to that value is fetched. Syntax: public int getModifiers() Return Va
    3 min read
  • Method Class | getReturnType() Method in Java
    Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2The java.lang.reflectMethod Class help in getting information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. The getReturnType
    4 min read
  • Method Class | getDefaultValue() Method in Java
    The getDefaultValue() method of java.lang.reflect.Method class. It returns the default value for the annotation member represented by the Method object. When annotation member belongs to primitive types, then the method returns the instance of that wrapper type. It returns null if annotation member
    3 min read
  • Method Class | getDeclaringClass() method in Java
    The java.lang.reflect.Method.getDeclaringClass() method of Method class returns the Class object for the class or interface which defines the method on which we are applying this getDeclaringClass() method. Syntax: public Class<?> getDeclaringClass() Return Value: This method returns the Class
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences