您的位置:首页 > 编程语言 > Java开发

Java泛型 通过反射获得方法参数中的变量类名和泛型

2015-07-09 20:31 525 查看
通过反射获得方法参数中的变量类名和泛型

package test;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;

public class MainTest {
public static void main(String[] args) throws Exception {
ArrayList<String> a = new ArrayList<String>();
Method applyMethod = MainTest.class.getMethod("applyMethod", ArrayList.class);
//applyMethod.invoke(null, a);
ParameterizedType pt = (ParameterizedType) applyMethod.getGenericParameterTypes()[0];
System.out.println(pt.getRawType()); //ArrayList
System.out.println(pt.getActualTypeArguments()[0]); //String
}

public static void applyMethod(ArrayList<String> a) {

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: