java反射基本知识


泛型基本:5.0 泛型出现之后,Java Reflection API的新特点。首先增加一个接口java.lang.reflect.Type
  java.lang.reflect.Type
|--------TypeVariable
|--------ParameterizedType
|--------GenericArrayType
|--------WildcardType
1) TypeVariable:用来保存泛型 编译前的基本信息

  1. public class BaseOrder<extends Object & Serializable,N extends Comparable<N>> implements IBaseOrder {  
  2.     public M getManufactory(){  
  3.         return manufactory;  
  4.     }  

  5. 主要是继承一个类,实现多个接口,getBounds去获取
  1. assertTrue("The type of field manufactory is an instance of TypeVariable", type instanceof TypeVariable);  
  2. TypeVariable tType = (TypeVariable)type;  
  3. assertEquals("The name of this TypeVariable is M", "M", tType.getName());  
  4. assertEquals("The TypeVariable bounds two type", 2, tType.getBounds().length);   
  5. assertEquals("One type of these bounds is Object", Object.class, tType.getBounds()[0]);   
  6. assertEquals("And annother si Serializable", Serializable.class, tType.getBounds()[1]);


2) ParameterizedType 带参的泛型,主要用于继承,获取泛型用

  1. public class Order  
  2.        extends BaseOrder<Customer, Long> implements IOrder, Serializable {  
  3. }  
  1. Type genericSuperclass = Order.class.getGenericSuperclass();  
  2. assertTrue("Order's supper class is a type of ParameterizedType.", genericSuperclass instanceof ParameterizedType);  
  3. ParameterizedType pType = (ParameterizedType)genericSuperclass;  
  4. assertEquals("Order's supper class is BaseOrder.", BaseOrder.class, pType.getRawType());  
  5. Type[] arguments = pType.getActualTypeArguments();  
  6. assertEquals("getActualTypeArguments() method return 2 arguments.", 2, arguments.length);  
  7. for (Type type : arguments) {  
  8.     Class clazz = (Class)type;  
  9.     if(!(clazz.equals(Customer.class)) && !(clazz.equals(Long.class))){  
  10.         assertTrue(false);  
  11.     }  
  12. }  

getGenericSuperclass :获取父类的泛型
getRawType() :             获取该泛型对应的设计类
getActualTypeArguments : 获取该泛型实际传入的类型


3) GenericArrayType 泛型数组

  • public String[] getPayments(String[] payments, List<Product> products){  
  •     return payments;  
  • }

  • Method getPayments = BaseOrder.class.getMethod("getPayments", new Class[]{String[].class, List.class});  
  • types = getPayments.getGenericParameterTypes();  
  • assertTrue("The first parameter of this method is GenericArrayType.", types[0] instanceof GenericArrayType);  
  • GenericArrayType gType = (GenericArrayType)types[0];  
  • assertEquals("The GenericArrayType's component is String.", String.class, gType.getGenericComponentType());



4)WildcardType 就是获取通配符泛型的信息

  1. private Comparable<? extends Customer> comparator;  
  1. Field comparatorField = BaseOrder.class.getDeclaredField("comparator");       
  2. ParameterizedType pType = (ParameterizedType)comparatorField.getGenericType();  
  3. type = pType.getActualTypeArguments()[0];  
  4. assertTrue("The type of field comparator is an instance of ParameterizedType, and the actual argument is an instance of WildcardType.", type instanceof WildcardType);  
  5. WildcardType wType = (WildcardType)type;  
  6. assertEquals("The upper bound of this WildcardType is Customer.", Customer.class, wType.getUpperBounds()[0]);  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值