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.
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:
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.
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.
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]
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.