43 _ obtain the actual type parameters of the generic type through reflection, 43 type parameters

Source: Internet
Author: User

43 _ obtain the actual type parameters of the generic type through reflection, 43 type parameters

What frameworks often do,

Obtain the data type based on the method signature, convert it to the corresponding object, and fill it in.

 

Package com. itcast. day2; import java. lang. reflect. method; import java. lang. reflect. parameterizedType; import java. lang. reflect. type; import java. util. arrayList; import java. util. collection; import java. util. date; import java. util. vector; import com. itcast. day1.ReflectPoint; public class GenericExc {public static void main (String [] args) throws Exception {Method applyMethod = GenericExc. class. getMethod ("apply Vector ", Vector. class); // Type is a common high-level interface of all types in Java programming language. They include the original type, parameterized type, array type, type variable, and basic type. Type [] types = applyMethod. getGenericParameterTypes (); ParameterizedType pType = (ParameterizedType) types [0]; // ParameterizedType is a Type sub-interface System. out. println (pType. getRawType (); // obtain the original type System. out. println (pType. getActualTypeArguments () [0]); // get the actual type // when applyVector (Vector v), java. lang. class
// ApplyVector (Vector <Date> v) sun. reflect. generics. reflectiveObjects. parameterizedTypeImpl System. out. println (types [0]. getClass (); // After jdk1.8, a simple method is provided to directly obtain java. util. vector <java. util. date> System. out. println (types [0]. getTypeName ();} public void applyVector (Vector <Date> v ){}}

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.