How to obtain a generic type parameter

Source: Internet
Author: User

We cannot get the date type from the vector<date> vector = new vector<date> () but we can get the type of date in the following way

1. Write a method that side of the following fit Word

public class Itcastannotation {

public static void Main (String args[]) {

2. Get the byte code of this class

Class clazz = Itcastannotation.class;

3. Obtain the method object corresponding to the GetType methods for obtaining the class by reflection
Method method = Clazz.getdeclaredmethod ("GetType", Vector.class);

4 by looking up a jdk1.5 document, you can send a method of getgenericparametertypes () to obtain the parameters of an enumerated type, and return an array
type[] Type = Method.getgenericparametertypes ();

Force the downward type to be converted into his subclass
Parameterizedtype Typep = (parameterizedtype) type[0];

5jdk.1.5 the Getrawtyep () method to obtain the source type
System.out.println (Typep.getrawtype ());

6getActualTypeArguments () Gets an array of type parameters, and the first print gets the type of date
System.out.println (typep.getactualtypearguments () [0]);
}
private static void GetType (Vector<date> Vector) {

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.