利用反射取得泛型信息

一、传统通过反射取得函数的参数和返回值

None.gif import  java.lang.reflect.Method;
None.gif
import  java.lang.reflect.ParameterizedType;
None.gif
import  java.lang.reflect.Type;
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.Map;
None.gif
import  java.util.List;
None.gif
import  java.util.Set;
None.gif 
None.gif
None.gif
import  sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
None.gif 
ExpandedBlockStart.gifContractedBlock.gif
public   class  Foo dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  main(String[] args)  throws  Exception  dot.gif {
InBlock.gif     Method[] methods 
=  Foo. class .getDeclaredMethods();
ExpandedSubBlockStart.gifContractedSubBlock.gif     
for (Method method : methods) dot.gif {
InBlock.gif      Class[] paramTypeList 
=  method.getParameterTypes();
InBlock.gif      
InBlock.gif      Class returnType 
=  method.getReturnType();
InBlock.gif      System.out.println(returnType);
ExpandedSubBlockStart.gifContractedSubBlock.gif      
for (Class clazz:paramTypeList) dot.gif {
InBlock.gif          System.out.println(clazz);
ExpandedSubBlockEnd.gif      }

InBlock.gif      System.out.println();
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static  String test1(String str) dot.gif {
InBlock.gif     
return   null ;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static  Integer test2(String str,Integer i) dot.gif {
InBlock.gif     
return   null ;
ExpandedSubBlockEnd.gif    }

InBlock.gif   
ExpandedBlockEnd.gif}

None.gif
None.gif


二、在有泛型的时候,取得参数和返回值的集合类的泛型信息

None.gif import  java.lang.reflect.Method;
None.gif
import  java.lang.reflect.ParameterizedType;
None.gif
import  java.lang.reflect.Type;
None.gif
import  java.util.ArrayList;
None.gif
import  java.util.Map;
None.gif
import  java.util.List;
None.gif
import  java.util.Set;
None.gif
None.gif
import  sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Foo  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  main(String[] args)  throws  Exception  dot.gif {
InBlock.gif        Method[] methods 
=  Foo. class .getDeclaredMethods();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for  (Method method : methods)  dot.gif {
InBlock.gif            System.out.println(
" returnType: " );
InBlock.gif            Type returnType 
=  method.getGenericReturnType();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if  (returnType  instanceof  ParameterizedType) dot.gif {
InBlock.gif                Type[] types 
=  ((ParameterizedType)returnType).getActualTypeArguments();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
for (Type type:types) dot.gif {
InBlock.gif                    System.out.println(type);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            System.out.println(
" paramTypeType: " );
InBlock.gif            Type[] paramTypeList 
=  method.getGenericParameterTypes();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for  (Type paramType : paramTypeList)  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if  (paramType  instanceof  ParameterizedType) dot.gif {
InBlock.gif                    Type[] types 
=  ((ParameterizedType)paramType).getActualTypeArguments();
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
for (Type type:types) dot.gif {
InBlock.gif                        System.out.println(type);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static  List < String >  test3(List < Integer >  list)  dot.gif {
InBlock.gif        
return   null ;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private   static  Map < String, Double >  test4(Map < String, Object >  map)  dot.gif {
InBlock.gif        
return   null ;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

三、应用环境
例如你要写一个程序,需求把一个如下的配置文件变成一个集合类。

None.gif < config name = " Foo.DoubleBean " >
None.gif    
< element key = " key1 "  value = " 1.1 " />
None.gif    
< element key = " key2 "  value = " 2.2 " />
None.gif    
< element key = " key3 "  value = " 3.3 " />
None.gif
</ config >

根据用户的参数变成不同的集合类 Map<String.String>     Map<String,Double>   Map<String,Float>
如果你要着手开发一个框架,这样的需求会比较常见。这个时候取到setXX()函数的参数,就可以对应上边的问题了。

99194.html

dreamstone 2007-02-10 23:24 发表评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值