zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.shlanbao.tzsc.utils.tools;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import com.shlanbao.tzsc.utils.extents.DateFmtAnnotation;
import com.shlanbao.tzsc.utils.extents.NumberFmtAnnotation;
/**
 * Bean转换器
 * <li>@author Leejean
 * <li>@create 2014-6-24 上午11:48:40
 * <li>本转换器支持自定义日期转换 
 * <li>使用方法直接在成员变量加自定义注解即可
 * <li>例如:
 * <li>@DateFmtAnnotation(fmtPattern="yyyy-MM-dd HH:mm:ss")
 * <li>private String modifyDatetime;
 * <li>后期可拓展数字精度
 * <li>@NumberFmtAnnotation(scale=1)
 * <li>private Double num;
 */
public class BeanConvertor { 
    /**
     * 将source中的同名属性的值复制到desc的同名属性中
     * @author Leejean
     * @param source 源
     * @param target 目标
     */
    public static void copyProperties(Object source, Object target) throws Exception{
        if(source == null) return;        
        handle(source, target);        
    }
    /**
     * 将source中的同名属性赋值到另外一个指定类型的对象中,并返回,
     * @author Leejean
     * @create 2014-7-8下午06:45:01
     * @param <T> 目标类型
     * @param source 员
     * @param clazz 目标类.class
     * @return 目标类型对象
     * @throws Exception 转换异常
     */
    @SuppressWarnings("unchecked")
    public static <T> T copyProperties(Object source,Class<T> clazz) throws Exception{
        if(source == null) return null;        
        Object target=clazz.newInstance();
        if (HbUtils.isProxy(source)){
            source = HbUtils.deproxy(source);
        }
        return (T) handle(source, target);
    }
    /**
     * 针对集合的操作
     * @author Leejean
     * @param srcList 源集合
     * @param clazz 目标类型
     * @return 目标集合
     * @throws Exception  转换异常
     */
    @SuppressWarnings("rawtypes")
    public static <T> List<T> copyList(List srcList, Class<T> clazz) throws Exception{
        List<T> descList=null;
        try{
            descList = new ArrayList<>();
            if(srcList == null) return descList;
            for(Object o : srcList){
                T t = copyProperties(o, clazz);
                descList.add(t);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return descList;
    }
    /**
     * 转换处理
     * @author Leejean
     * @create 2014-7-8下午06:44:10
     * @param source 源
     * @param target 目标
     * @return 处理结果
     * @throws Exception 转换异常
     */
    private static Object handle(Object source,Object target) throws Exception{
        Field[] sourceFields=source.getClass().getDeclaredFields();
        Class targetClass=target.getClass();
        for (Field field : sourceFields) {
            field.setAccessible(true);
            Object object=field.get(source);
            if(object!=null){
                Field targetFiled=null;
                try {
                    targetFiled = targetClass.getDeclaredField(field.getName());
                } catch (Exception e) {
                    continue;
                }            
                targetFiled.setAccessible(true);
                if(targetFiled!=null){//属性存在
                    boolean isDateAnnotationPresent=targetFiled.isAnnotationPresent(DateFmtAnnotation.class);
                    if(isDateAnnotationPresent){//被日期注解修饰
                        //得到注解内容
                        String pattern=targetFiled.getAnnotation(DateFmtAnnotation.class).fmtPattern();
                        //获得目标对象属性类型,以便将数据源赋值
                        Class targetFiledType=targetFiled.getType();
                        Class sourceFiledType=field.getType();
                        if(targetFiledType==Date.class&&sourceFiledType==String.class){
                            
                            object=DateUtil.formatStringToDate(object.toString(), pattern);
                            targetFiled.set(target,object);
                            
                        }else if(targetFiledType==String.class&&sourceFiledType==Date.class){
                            
                            object=DateUtil.formatDateToString((Date)object, pattern);
                            targetFiled.set(target,object);
                        }
                    }else{
                        boolean isNumberAnnotationPresent=targetFiled.isAnnotationPresent(NumberFmtAnnotation.class);
                        if(isNumberAnnotationPresent){//被数字格式化注解修饰
                            //得到注解内容
                            int scale=targetFiled.getAnnotation(NumberFmtAnnotation.class).scale();
                            Class targetFiledType=targetFiled.getType();
                            Class sourceFiledType=field.getType();
                            if(targetFiledType==Double.class&&sourceFiledType==Double.class){
                                object=MathUtil.roundHalfUp((Double)object, scale);
                                targetFiled.set(target,object);
                            }else{
                                targetFiled.set(target,object);
                            }
                        }else{
                            //没有被注解的直接赋值                            
                            targetFiled.set(target,object);
                        }
                    }
                }
                
            }
        }
        return target;
    }
 
    public static void main(String[] args)  {
        
        /*SysUser source=new SysUser(1L, DateUtil.formatStringToDate("2012-01-01 11:11:11"),DateUtil.formatStringToDate("2012-01-01 11:11:11"), "111", "111"); 
        UserBean target= new UserBean(2L, "2022-02-21 22:22:22", "2022-02-21 22:22:22", "222", "222");*/
        /*SysUser source=new SysUser(); 
        source.setA(1.2813);
        UserBean target= new UserBean();
        target.setA(4.223298798776);
        List<UserBean> userlist=new ArrayList<UserBean>();
        userlist.add(target);
        try {
            System.out.println(BeanConvertor.copyProperties(target, SysUser.class).toString());
            for (SysUser sysUser : BeanConvertor.copyList(userlist, SysUser.class)) {
                //System.out.println(sysUser.toString());
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        /*List<SysUser> users=new ArrayList<SysUser>();
        users.add(new SysUser(new Date(), new Date(), "yyy", "11"));
        users.add(new SysUser(new Date(), new Date(), "xxx", "22"));
        List<UserBean> beanusers=BeanConvertor.copyList(users, UserBean.class);
        for (UserBean userBean : beanusers) {
            System.out.println(userBean.toString());
        }*/
        
    }
 
 
}