Java反射中的范型相关类

项目网络框架优化用到了范型,同事在代码中用到了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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值