梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
package com.roadway.keyin.bo.user.util;

import java.lang.reflect.Field;

import com.roadway.keyin.dao.hibernate.PreviewTemplate;
import com.roadway.keyin.dao.hibernate.PreviewUser;

public class SeterUtil
{
    public static Object set(Object sourceObj, Object targetObj)
    
{
        
if (sourceObj == null || targetObj == null)
        
{
            
return targetObj;
        }

        
        Class
<?> sourceObjCls = sourceObj.getClass();
        Field[] targetObjFields 
= targetObj.getClass().getFields();
        
        Field sourceObjField 
= null;
        
for (Field targetObjField : targetObjFields)
        
{
            
try
            
{
                sourceObjField 
= sourceObjCls
                        .getField(targetObjField.getName());
                
if (sourceObjField == null)
                
{
                    
continue;
                }

                
                
/** 把属性名和属性类型加在一起 */
                String sourceStr 
= sourceObjField.getType().toString();
                sourceStr 
+= sourceObjField.getName();
                
                
/** 把属性名和属性类型加在一起 */
                String targetStr 
= targetObjField.getType().toString();
                targetStr 
+= targetObjField.getName();
                
                
/** 比较两个属性是否相等 *** */
                
if (sourceStr.equals(targetStr))
                
{
                    targetObjField
                            .set(targetObj, sourceObjField.get(sourceObj));
                }

                
else
                
{
                    System.out.println(sourceStr);
                    System.out.println(targetStr);
                    System.out.println();
                }

            }

            
catch (Exception ex)
            
{}
        }

        
return targetObj;
    }

    
    public static 
void main(String[] args) throws IllegalArgumentException,
            SecurityException, IllegalAccessException, NoSuchFieldException
    
{
        PreviewUser privewUser 
= new PreviewUser();
        PreviewTemplate previewTemplate 
= new PreviewTemplate();
        previewTemplate.setP100(15624L);
        
        SeterUtil.set(previewTemplate, privewUser);
        
        System.out.println(privewUser.getP100());
    }

}

posted on 2009-10-29 18:11 HUIKK 阅读(177) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: