反射在许多框架中都很常见。反射虽然降低了运行速度,但是提高了开发速度;本小节主要展示通过反射操作泛型。
一.案例涉及的类和接口
1.父类
package reflect.parameter;
public class Car <T> {
}
2.接口
package reflect.parameter;
public interface CarInterface<T> {
}
3.泛型类
public class Configure {
}
4.实现类
package reflect.parameter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Benz extends Car<Configure> implements CarInterface<Configure>{
public void showInfo(Map<String, String> infoMap, List<String> infoList){
}
public List<String> getInfo(){
return new ArrayList<String>();
}
public void getExceptionInfo() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d://");
}
}
二,测试
package reflect.parameter;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
/**
* 反射操作泛型
*/
public class ParameterTypeTest {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
System.out.println("==============================方法参数化类型======================");
getMethodParameterType();
System.out.println("=============================返回值参数化类型=========================================");
getReturnParameterType();
System.out.println("==================================父类参数化类型====================================");
getParentParameterType();
System.out.println("===============================获取异常类型================");
getExceptionType();
System.out.println("===============================接口参数化类型===================");
getInterfaceParameterType();
}
/**
* 获取方法参数的参数化类型
* @throws ClassNotFoundException
* @throws NoSuchMethodException
*/
private static void getMethodParameterType() throws ClassNotFoundException, NoSuchMethodException {
Class benzClass = Class.forName("reflect.parameter.Benz");
Method showInfo = benzClass.getMethod("showInfo", Map.class, List.class);
Type[] generiticParameterTypes = showInfo.getGenericParameterTypes();
for(Type generiticParameterType : generiticParameterTypes){
System.out.println("类型:" + generiticParameterType);
if(generiticParameterType instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType)generiticParameterType).getActualTypeArguments();
for(Type actualTypeArgument : actualTypeArguments){
System.out.println("----------参数化类型" + actualTypeArgument);
}
}
}
}
/**
* 获取返回值的参数化类型
* @throws NoSuchMethodException
*/
private static void getReturnParameterType() throws NoSuchMethodException {
Class benz = Benz.class;
Method getInfo = benz.getMethod("getInfo");
Type returnType = getInfo.getGenericReturnType();
if(returnType instanceof ParameterizedType){
System.out.println("类型:" + returnType);
Type[] actualTypeArguments = ((ParameterizedType)returnType).getActualTypeArguments();
for(Type actualTypeArgument : actualTypeArguments){
System.out.println("-------------返回值的参数化类型" + actualTypeArgument);
}
}
}
/**
* 获取父类的参数化类型 必须是继承
*/
private static void getParentParameterType(){
Benz benz = new Benz();
Class benzClass = benz.getClass();
Type genericSuperclass = benzClass.getGenericSuperclass();
System.out.println("类型" + genericSuperclass);
if(genericSuperclass instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType)genericSuperclass).getActualTypeArguments();
for(Type actualTypeArgument : actualTypeArguments){
System.out.println("父类参数化类型" + actualTypeArgument);
}
}
}
/**
* 获取方法抛出的异常类型
* @throws NoSuchMethodException
*/
private static void getExceptionType() throws NoSuchMethodException {
Class benzClass = Benz.class;
Method exception = benzClass.getMethod("getExceptionInfo");
Type[] genericExceptionTypes = exception.getGenericExceptionTypes();
for(Type genericExceptionType : genericExceptionTypes){
System.out.println("类型" + genericExceptionType);
}
}
/**
* 获取接口的参数类型
*/
private static void getInterfaceParameterType(){
Class benzClass = Benz.class;
Type[] genericInterfaces = benzClass.getGenericInterfaces();
for(Type genericInterface : genericInterfaces){
System.out.println("类型" + genericInterface);
if(genericInterface instanceof ParameterizedType){
Type[] actualTypeArguments = ((ParameterizedType)genericInterface).getActualTypeArguments();
for(Type actualTypeArgument : actualTypeArguments){
System.out.println("接口参数化类型" + actualTypeArgument);
}
}
}
}
}
三,控制台输出
方法参数化类型
类型:java.util.Map<java.lang.String, java.lang.String>
----------参数化类型class java.lang.String
----------参数化类型class java.lang.String
类型:java.util.List<java.lang.String>
----------参数化类型class java.lang.String
=返回值参数化类型=====
类型:java.util.List<java.lang.String>
-------------返回值的参数化类型class java.lang.String
父类参数化类型==
类型reflect.parameter.Car<reflect.parameter.Configure>
父类参数化类型class reflect.parameter.Configure
===============获取异常类型
类型class java.io.FileNotFoundException
=============接口参数化类型=
类型reflect.parameter.CarInterface<reflect.parameter.Configure>
接口参数化类型class reflect.parameter.Configure