Obtain generic information through reflection.

Source: Internet
Author: User

Obtain generic information through reflection.

 

 

1 package com. coscon. reflectionTest; 2 3 import java. lang. reflect. method; 4 import java. lang. reflect. parameterizedType; 5 import java. lang. reflect. type; 6 import java. util. list; 7 import java. util. map; 8 9 // obtain generic information through reflection 10 public class ReflectionForGenerics {11 public void test01 (Map <String, User> map, List <User> users) {12 System. out. println ("test01"); 13} 14 15 public Map <Integer, User> test02 () {16 System. out. println ("test02"); 17 return null; 18} 19 20 public static void main (String [] args) {21 try {22 Class clazz = Class. forName ("com. coscon. reflectionTest. reflectionForGenerics "); 23 // obtain the generic information of the specified Method parameter 24 method = clazz. getMethod ("test01", Map. class, List. class); 25 // obtain the generic parameter 26 Type [] types = method. getGenericParameterTypes (); 27 for (Type type: types) {28 System. out. println ("#" + type); 29 if (type instanceof ParameterizedType) {30 Type [] actualTypeArguments = (ParameterizedType) type ). getActualTypeArguments (); 31 for (Type type2: actualTypeArguments) {32 System. out. println ("generic parameter type:" + type2); 33} 34} 35} 36 37 System. out. println ("--------------------------------------------------------------"); 38 Method method2 = clazz. getMethod ("test02", null); 39 // obtain the returned value generic information 40 Type returnType = method2.getGenericReturnType (); 41 System. out. println ("#" + returnType); 42 if (returnType instanceof ParameterizedType) {43 Type [] typeArguments = (ParameterizedType) returnType ). getActualTypeArguments (); 44 for (Type: typeArguments) {45 System. out. println ("returned value generic type:" + type); 46} 47} 48} catch (Exception e) {49 // TODO Auto-generated catch block50 e. printStackTrace (); 51} 52} 53}

Print results

# Java. util. Map <java. lang. String, com. coscon. reflectionTest. User>
Generic parameter type: class java. lang. String
Generic parameter type: class com. coscon. reflectionTest. User
# Java. util. List <com. coscon. reflectionTest. User>
Generic parameter type: class com. coscon. reflectionTest. User
--------------------------------------------------------------
# Java. util. Map <java. lang. Integer, com. coscon. reflectionTest. User>
Returned value generic type: class java. lang. Integer
Returned value generic type: class com. coscon. reflectionTest. User

 

 

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.