1 package common;
 2 /* 
 3  * Created on 2007-8-21
 4  */
 5 
 6 import java.beans.BeanInfo;
 7 import java.beans.Introspector;
 8 import java.beans.PropertyDescriptor;
 9 
10 import com.ebuilds.pmzls.project.dto.TCmBaseDicttypeDto;
11 import com.ebuilds.pmzls.project.ejb.cmp.TCmBaseDicttype;
12 /**
13  * 利用jdk 內省機制,將dto轉換未cmp
14  * 當前只支持3個類型
15  * dto to cmp
16  * @author liuyang
17  */
18 public class Convert {
19     public static void convertpojo(Object obj,Object objto,String noconvertcol) throws Exception
20     {
21 //        得到obj對象
22         BeanInfo bi = Introspector.getBeanInfo(obj.getClass(),Object.class );
23 //        得到objto對象
24         BeanInfo bi1 = Introspector.getBeanInfo(objto.getClass(),Object.class );
25 //        取出obj中所有具有get和set方法的變量名
26         PropertyDescriptor[] props = bi.getPropertyDescriptors();
27 //        取出objto中所有具有get和set方法的變量名
28         PropertyDescriptor[] props1 = bi1.getPropertyDescriptors();
29 //        按變量名進行索引
30         for ( int i=0;i<props.length;i++){
31 //            排序對比相同則轉換
32             for(int j=0;j<props1.length;j++)
33             {
34 //                是否是不需要轉換字段
35                 boolean bf = true;
36 //                取出obj中該變量所對應的值
37                 Object oth = props[i].getReadMethod().invoke(obj,null);
38 //                如果是不需要轉換變量則不進行取值
39                 for(String name:noconvertcol)
40                 {
41                     if(name.equals(props[i].getName()))
42                     {
43                         bf=false;
44                         break;
45                     }
46                 }
47 //                判斷是否是相同類型,相同名稱,并且是需要轉換的變量
48                 if(props[i].getName().equals(props1[j].getName()) && props[i].getPropertyType().equals(props1[j].getPropertyType()) && bf)
49                 {
50 //                    判斷變量類型,將空轉換為0或者“”
51                     if(props[i].getPropertyType().equals(java.lang.Long.class))
52                     {
53                         props1[j].getWriteMethod().invoke(objto,(oth==null?Long.valueOf(0):oth));    
54                     }else if(props[i].getPropertyType().equals(java.lang.String.class)){
55                         props1[j].getWriteMethod().invoke(objto,(oth==null?"":oth));
56                     }else if(props[i].getPropertyType().equals(java.lang.Integer.class))
57                     {
58                         props1[j].getWriteMethod().invoke(objto,(oth==null?0:oth));
59                     }
60                 }
61             }
62         }
63 //        for ( int i=0;i<props1.length;i++){
64 //            System.out.println(props1[i].getName()+ "=" +
65 //                    props1[i].getReadMethod().invoke(objto, null ));
66 //        }
67     }
68     
69     
70     
71     public static void main(String[] args) throws Exception{
72         // 如果不想把父類的屬性也列出來的話,
73         // 那 getBeanInfo 的第二個參數填寫父類的信息
74         TCmBaseDicttypeDto dto = new TCmBaseDicttypeDto();
75         dto.setCode("00001");
76         dto.setName("00002");
77         TCmBaseDicttype td = new  TCmBaseDicttype();
78         convertpojo(dto, td);
79     }    
80     
81        
82 }
83 
最近剛剛看了一些關于內省的文章正好開發項目中要使用,所以自己寫了一個轉換器,供大家一起學習
TCmBaseDicttypeDto、TCmBaseDicttype只是一個set和get集合

我的文章大家看了都沒給回復,大哥們給點人氣。