项目网络框架优化用到了范型,同事在代码中用到了fastJson的TypeReference类,了解到一些Java反射中的范型相关接口:Type,ParameterizedType,GenericDeclaration,TypeVariable。
1.Type
Type is the common superinterface for all types in the Java programming language. These include raw types, parameterized types, array types, type variables and primitive types.
Type是java编程语言中所用类型的公共实现接口,实现该接口的有原始类型,参数化类型,数组类型,类型变量和基本类型
2.GenericDeclaration
A common interface for all entities that declare type variables.
从字面意思理解:范型声明 Class<T> 实现了该接口
TypeVariable<?>[] getTypeParameters();返回范型声明中的类型变量 如Map<K,V> 返回数组中存储K,V
3.ParameterizedType
ParameterizedType represents a parameterized type such as Collection<String>;
参数化类型这类的类型指已经指明范型参数的具体类型
Type[] getActualTypeArguments();
Type getRawType(); 原始类型 :如Class<T>的实例类型Class<String>的RawType是Class
Type getOwnerType(); 范型类是内部类
4.TypeVariable<D extends GenericDeclaration>
TypeVariable is the common superinterface for type variables of kinds.
D getGenericDeclaration();
Type[] getBounds();
String getName();
注意:范型声明中的T,K,V类型变量与实例化参数类型Map<String,Integer>中的String,Integer的名称区别,前者是TypeParameter,后者是TypeArgument
import java.lang.*;
import java.lang.reflect.*;
public class Template<T>{
public T member;
public Template(){
Type type = getClass().getGenericSuperclass();
//System.out.println("Template getClass()==" + getClass());
//System.out.println("Template type = " + type);
}
}
import java.lang.reflect.*;
public class TemplateChild extends Template<String>{
}
import java.lang.*;
import java.lang.reflect.*;
public class Main{
public static void main(String[] args){
TemplateChild temp=new TemplateChild();
System.out.println("getClass()==" + temp.getClass());
System.out.println("type = " + temp.getClass().getGenericSuperclass());
Type trueType = ((ParameterizedType)temp.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
System.out.println("typeArgument = " + trueType);
System.out.println("rawType = " +((ParameterizedType)temp.getClass().getGenericSuperclass()).getRawType());
Template<String> t=new Template();
System.out.println("----Veriable----"+t);
TypeVariable[] v=((GenericDeclaration) t.getClass()).getTypeParameters();
System.out.println("---Class Object---"+t.getClass());
System.out.println("---TypeVariable---"+v[0]+"---"+v.length);
System.out.println("---GenericDeclaration---"+v[0].getGenericDeclaration());
Type[] bounds=v[0].getBounds();
System.out.println("---TypeVariable bound--bound size-"+bounds.length+"-----"+bounds[0]);
System.out.println("---TypeVar Name---"+v[0].getName());
}
}
运行结果:
seedinwinddeMacBook-Pro:javaTest seedinwind$ java Main
getClass()==class TemplateChild
type = Template<java.lang.String>
typeArgument = class java.lang.String
rawType = class Template
----Veriable----Template@7852e922
---Class Object---class Template
---TypeVariable---T---1
---GenericDeclaration---class Template
---TypeVariable bound--bound size-1-----class java.lang.Object
---TypeVar Name---T
把Template<T>改为Template<S> 可见TypeVariable 是声明中的类型变量
输出
seedinwinddeMacBook-Pro:javaTest seedinwind$ java Main
getClass()==class TemplateChild
type = Template<java.lang.String>
typeArgument = class java.lang.String
rawType = class Template
----Veriable----Template@7852e922
---Class Object---class Template
---TypeVariable---S---1
---GenericDeclaration---class Template
---TypeVariable bound--bound size-1-----class java.lang.Object
---TypeVar Name---S
参考:java.lang.reflect
java 范型官方文档 http://docs.oracle.com/javase/tutorial/java/generics/index.html