CopyUtil拷贝对象

public class CopyUtil {
    public CopyUtil() {
    }

    public static void copyToStr(Object dest, Object source) throws Exception {
        BeanInfo sourceInfo = Introspector.getBeanInfo(source.getClass());
        BeanInfo destInfo = Introspector.getBeanInfo(dest.getClass());
        PropertyDescriptor[] sce = sourceInfo.getPropertyDescriptors();
        PropertyDescriptor[] dec = destInfo.getPropertyDescriptors();
        PropertyDescriptor[] var9 = sce;
        int var8 = sce.length;

        for(int var7 = 0; var7 < var8; ++var7) {
            PropertyDescriptor s = var9[var7];
            Method getMethod = s.getReadMethod();
            if (getMethod != null) {
                try {
                    String name = s.getName();
                    Object result = getMethod.invoke(source);
                    PropertyDescriptor[] var16 = dec;
                    int var15 = dec.length;

                    for(int var14 = 0; var14 < var15; ++var14) {
                        PropertyDescriptor d = var16[var14];
                        if (name.equalsIgnoreCase(d.getName()) && result != null && !"".equals(result)) {
                            Method setMethod = d.getWriteMethod();
                            if (setMethod != null) {
                                Object obj = objToStr(s, source);
                                setMethod.invoke(dest, obj);
                            }
                        }
                    }
                } catch (IllegalArgumentException var19) {
                    var19.printStackTrace();
                } catch (IllegalAccessException var20) {
                    var20.printStackTrace();
                } catch (InvocationTargetException var21) {
                    var21.printStackTrace();
                }
            }
        }

    }

    public static void copyToObj(Object dest, Object source) throws IntrospectionException {
        BeanInfo sourceInfo = Introspector.getBeanInfo(source.getClass());
        BeanInfo destInfo = Introspector.getBeanInfo(dest.getClass());
        PropertyDescriptor[] sce = sourceInfo.getPropertyDescriptors();
        PropertyDescriptor[] dec = destInfo.getPropertyDescriptors();
        PropertyDescriptor[] var9 = sce;
        int var8 = sce.length;

        for(int var7 = 0; var7 < var8; ++var7) {
            PropertyDescriptor s = var9[var7];
            Method getMethod = s.getReadMethod();
            if (getMethod != null) {
                try {
                    String name = s.getName();
                    Object result = getMethod.invoke(source);
                    PropertyDescriptor[] var16 = dec;
                    int var15 = dec.length;

                    for(int var14 = 0; var14 < var15; ++var14) {
                        PropertyDescriptor d = var16[var14];
                        if (name.equalsIgnoreCase(d.getName()) && result != null && !"".equals(result)) {
                            Method setMethod = d.getWriteMethod();
                            if (setMethod != null) {
                                Object obj = strToObj(s, source, d);
                                setMethod.invoke(dest, obj);
                            }
                        }
                    }
                } catch (IllegalArgumentException var19) {
                    var19.printStackTrace();
                } catch (IllegalAccessException var20) {
                    var20.printStackTrace();
                } catch (InvocationTargetException var21) {
                    var21.printStackTrace();
                } catch (Exception var22) {
                    var22.printStackTrace();
                }
            }
        }

    }

    public static void copyToObj2(Object dest, Object source) throws IntrospectionException {
        BeanInfo sourceInfo = Introspector.getBeanInfo(source.getClass());
        BeanInfo destInfo = Introspector.getBeanInfo(dest.getClass());
        PropertyDescriptor[] sce = sourceInfo.getPropertyDescriptors();
        PropertyDescriptor[] dec = destInfo.getPropertyDescriptors();
        PropertyDescriptor[] var9 = sce;
        int var8 = sce.length;

        for(int var7 = 0; var7 < var8; ++var7) {
            PropertyDescriptor p = var9[var7];
            Method getMethod = p.getReadMethod();
            if (getMethod != null) {
                try {
                    String name = p.getName();
                    Object result = getMethod.invoke(source);
                    PropertyDescriptor[] var16 = dec;
                    int var15 = dec.length;

                    for(int var14 = 0; var14 < var15; ++var14) {
                        PropertyDescriptor s = var16[var14];
                        if (s.getName() != null && name.equalsIgnoreCase(s.getName()) && result != null) {
                            Object obj = strConvert(s.getPropertyType(), result.toString());
                            Method setMethod = s.getWriteMethod();
                            if (setMethod != null) {
                                if (String.class.equals(s.getPropertyType())) {
                                    if (name.contains("Date")) {
                                        obj = Common.StringConversionString(obj.toString());
                                    } else if (name.contains("Time")) {
                                        obj = Common.StringConversion14String(obj.toString());
                                    }

                                    setMethod.invoke(dest, obj.toString());
                                } else if (Date.class.equals(p.getPropertyType())) {
                                    if (name.contains("Date")) {
                                        obj = Common.StringConversionString(obj.toString());
                                    } else if (name.contains("Time")) {
                                        obj = Common.StringConversion14String(obj.toString());
                                    }

                                    setMethod.invoke(dest, obj);
                                } else {
                                    setMethod.invoke(dest, obj);
                                }
                            }
                        }
                    }
                } catch (IllegalArgumentException var19) {
                    var19.printStackTrace();
                } catch (IllegalAccessException var20) {
                    var20.printStackTrace();
                } catch (InvocationTargetException var21) {
                    var21.printStackTrace();
                } catch (Exception var22) {
                    var22.printStackTrace();
                }
            }
        }

    }

    public static Object strToObj(PropertyDescriptor s, Object source, PropertyDescriptor d) throws Exception {
        Object obj = null;
        Method getMethod = s.getReadMethod();
        String str = (String)getMethod.invoke(source);
        if (Date.class.equals(d.getPropertyType())) {
            obj = Common.StringConversionDate(str);
        } else if (!Double.class.equals(d.getPropertyType()) && !Double.TYPE.equals(d.getPropertyType())) {
            if (!Integer.class.equals(d.getPropertyType()) && !Integer.TYPE.equals(d.getPropertyType())) {
                if (BigDecimal.class.equals(d.getPropertyType())) {
                    BigDecimal bObj = new BigDecimal(str);
                    obj = bObj.setScale(5, 4);
                } else if (String.class.equals(d.getPropertyType())) {
                    obj = str;
                }
            } else {
                obj = Integer.parseInt(str);
            }
        } else {
            obj = Double.parseDouble(str);
        }

        return obj;
    }

    public static String objToStr(PropertyDescriptor s, Object source) throws Exception {
        String sRet = null;
        Method getMethod = s.getReadMethod();
        Object obj = getMethod.invoke(source);
        if (Date.class.equals(s.getPropertyType())) {
            Date date = (Date)obj;
            SimpleDateFormat sdf;
            if (s.getName().contains("Date")) {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                sRet = sdf.format(date);
            } else if (s.getName().contains("Time")) {
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                sRet = sdf.format(date);
            }
        } else if (BigDecimal.class.equals(s.getPropertyType())) {
            BigDecimal b = new BigDecimal(obj.toString());
            sRet = Bigtostr(b);
        } else {
            sRet = obj.toString();
        }

        return sRet;
    }

    public static Object strConvert(Class<?> z, String text) {
        System.out.println("1111=" + z.getName() + "," + text);
        Object obj = null;
        if (text != null && !"".equals(text) && z != null) {
            if (!Double.class.equals(z) && !Double.TYPE.equals(z)) {
                if (!Integer.class.equals(z) && !Integer.TYPE.equals(z)) {
                    if (Date.class.equals(z)) {
                        obj = Common.StringConversionDate(text.toString());
                    } else if (BigDecimal.class.equals(z)) {
                        BigDecimal bObj = new BigDecimal(text);
                        obj = bObj.setScale(6, 4);
                    } else {
                        obj = text;
                    }
                } else {
                    obj = Integer.parseInt(text);
                }
            } else {
                obj = Double.parseDouble(text);
            }
        }

        return obj;
    }

    public static void main(String[] arg) {
        System.out.println(Bigtostr(new BigDecimal("12345.1234565")));
    }

    public static String Bigtostr(BigDecimal stock) {
        stock = stock.setScale(5, 4);
        return String.valueOf(stock);
    }

    public static void transCusCopyToStr(Object dest, Object source) throws Exception {
        BeanInfo sourceInfo = Introspector.getBeanInfo(source.getClass());
        BeanInfo destInfo = Introspector.getBeanInfo(dest.getClass());
        PropertyDescriptor[] sce = sourceInfo.getPropertyDescriptors();
        PropertyDescriptor[] dec = destInfo.getPropertyDescriptors();
        PropertyDescriptor[] var9 = sce;
        int var8 = sce.length;

        for(int var7 = 0; var7 < var8; ++var7) {
            PropertyDescriptor s = var9[var7];
            Method getMethod = s.getReadMethod();
            if (getMethod != null) {
                try {
                    String name = s.getName();
                    Object result = getMethod.invoke(source);
                    PropertyDescriptor[] var16 = dec;
                    int var15 = dec.length;

                    for(int var14 = 0; var14 < var15; ++var14) {
                        PropertyDescriptor d = var16[var14];
                        if (name.equalsIgnoreCase(d.getName()) && result != null && !"".equals(result)) {
                            Method setMethod = d.getWriteMethod();
                            if (setMethod != null) {
                                Object obj = transCusObjToStr(s, source);
                                setMethod.invoke(dest, obj);
                            }
                        }
                    }
                } catch (IllegalArgumentException var19) {
                    var19.printStackTrace();
                } catch (IllegalAccessException var20) {
                    var20.printStackTrace();
                } catch (InvocationTargetException var21) {
                    var21.printStackTrace();
                }
            }
        }

    }

    private static Object transCusObjToStr(PropertyDescriptor s, Object source) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        String sRet = null;
        Method getMethod = s.getReadMethod();
        Object obj = getMethod.invoke(source);
        if (Date.class.equals(s.getPropertyType())) {
            Date date = (Date)obj;
            if (s.getName().contains("Date")) {
                sRet = Common.DateConversionString(date);
            } else if (s.getName().contains("Time")) {
                sRet = Common.DateConversion14String(date);
            }
        } else if (BigDecimal.class.equals(s.getPropertyType())) {
            BigDecimal b = new BigDecimal(obj.toString());
            sRet = transCusBigtostr(b);
        } else {
            sRet = obj.toString();
        }

        return sRet;
    }

    private static String transCusBigtostr(BigDecimal stock) {
        stock = stock.setScale(5, 4);
        return String.valueOf(stock);
    }

    public static Object strConvert3(Class<?> z, Object text) {
        System.out.println("1111=" + z.getName() + "," + text);
        Object obj = null;
        if (text != null && !"".equals(text) && z != null) {
            if (!Double.class.equals(z) && !Double.TYPE.equals(z)) {
                if (!Integer.class.equals(z) && !Integer.TYPE.equals(z)) {
                    if (Date.class.equals(z)) {
                        obj = text;
                    } else if (BigDecimal.class.equals(z)) {
                        System.out.println(text.toString());
                        BigDecimal bObj = new BigDecimal(text.toString());
                        obj = bObj.setScale(6, 4);
                    } else {
                        obj = text;
                    }
                } else {
                    obj = Integer.parseInt(text.toString());
                }
            } else {
                obj = Double.parseDouble(text.toString());
            }
        }

        return obj;
    }

    public static void copyToObj3(Object dest, Object source) throws IntrospectionException {
        BeanInfo sourceInfo = Introspector.getBeanInfo(source.getClass());
        BeanInfo destInfo = Introspector.getBeanInfo(dest.getClass());
        PropertyDescriptor[] sce = sourceInfo.getPropertyDescriptors();
        PropertyDescriptor[] dec = destInfo.getPropertyDescriptors();
        PropertyDescriptor[] var9 = sce;
        int var8 = sce.length;

        for(int var7 = 0; var7 < var8; ++var7) {
            PropertyDescriptor p = var9[var7];
            Method getMethod = p.getReadMethod();
            if (getMethod != null) {
                try {
                    String name = p.getName();
                    Object result = getMethod.invoke(source);
                    PropertyDescriptor[] var16 = dec;
                    int var15 = dec.length;

                    for(int var14 = 0; var14 < var15; ++var14) {
                        PropertyDescriptor s = var16[var14];
                        if (s.getName() != null && name.equalsIgnoreCase(s.getName()) && result != null) {
                            Method setMethod = s.getWriteMethod();
                            if (setMethod != null) {
                                Object obj = strConvert3(s.getPropertyType(), result);
                                if (String.class.equals(s.getPropertyType()) && Date.class.equals(p.getPropertyType())) {
                                    Date date;
                                    if (name.contains("Date")) {
                                        date = (Date)obj;
                                        obj = Common.DateConversionString(date);
                                    } else if (name.contains("Time")) {
                                        date = (Date)obj;
                                        obj = Common.DateConversion14String(date);
                                    }

                                    setMethod.invoke(dest, obj.toString());
                                } else if (String.class.equals(p.getPropertyType()) && Date.class.equals(s.getPropertyType())) {
                                    Object obj = Common.StringConversionDate(obj.toString());
                                    setMethod.invoke(dest, obj);
                                } else {
                                    setMethod.invoke(dest, obj);
                                }
                            }
                        }
                    }
                } catch (IllegalArgumentException var20) {
                    var20.printStackTrace();
                } catch (IllegalAccessException var21) {
                    var21.printStackTrace();
                } catch (InvocationTargetException var22) {
                    var22.printStackTrace();
                } catch (Exception var23) {
                    var23.printStackTrace();
                }
            }
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值