通过反射去获取集合类中的泛型实际类型

package com.huaxia.utils.learn;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.Vector;

public class ClassLoaderTest {
	public static void main(String[] args) throws Exception {
//		// 从classpath根目录开始找配置文件
//		InputStream is = ClassLoaderTest.class.getClassLoader()
//				.getResourceAsStream("config.properties");
//		Properties prop = new Properties();
//		prop.load(is);
//		is.close();
//		System.out.println(prop.get("apple"));
		Method m = ClassLoaderTest.class.getMethod("applyVector", Vector.class,List.class);
		Type[] types = m.getGenericParameterTypes();
		for(Type t:types){
			System.out.println(((ParameterizedType) t).getRawType());
			System.out.println(((ParameterizedType) t).getActualTypeArguments()[0]);
		}
	}
	
	public static <T> void applyVector(Vector<Date> p,List<T> o){
		
	}
}

 执行结果:

class java.util.Vector

class java.util.Date

interface java.util.List

T

你可能感兴趣的:(集合类)