copvalue from wrapper type to primitive type
2016-06-18 09:33
417 查看
/** * <pre> * if the field of orig is wrapper, will skip it and would not copy it to dest. * in the meantime, the primitive filed should default value when dest object corresponding wrapper field value is null; * </pre> * * @param dest * @param orig */ void copyProperties(Object dest, Object orig) { Field[] fields = orig.getClass().getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); Class<?> fieldType = field.getType(); String fieldName = field.getName(); Object fieldValue = field.get(orig); // skip "orig" object's field that it's wrapper type and null; // and "dest" object's field that it's primitive type should be the default value automatically when "orig" object's filed value null; if (ClassUtils.isPrimitiveWrapper(fieldType) && fieldValue == null) { continue; } BeanUtils.copyProperty(dest, fieldName, fieldValue); } catch (IllegalAccessException e) { logger.error(e); } catch (InvocationTargetException e) { logger.error(e); } } }
result test:
@Test public void test01() throws InvocationTargetException, IllegalAccessException { Product02 product02 = new Product02(); product02.setName("name02"); product02.setProduct_int(123); product02.setProduct_double(4.56); product02.setPrice(new Amount("product type1111", new BigDecimal(2681))); Product01 product01 = new Product01(); System.out.println(product02); copyProperties(product01, product02); //BeanUtils.copyProperties(product01, product02); System.out.println(product01); }
Product02{name='name02', product_int=123, product_double=4.56, product_boolean=null, product_char=null, product_byte=null, product_long=null, price=Amount{productTpye='product type1111', price=2681}}
Product01{name='name02', product_int=123, product_double=4.56, product_boolean=false, product_char= , product_byte=0, product_long=0, product_float=0.0, price=Amount{productTpye='product type1111', price=2681}}
/** * <pre> * if the field of orig is wrapper, will skip it and would not copy it to dest. * in the meantime, the primitive filed should default value when dest object corresponding wrapper field value is null; * </pre> * * @param dest * @param orig */ void copyProperties(Object dest, Object orig) { Field[] fields = orig.getClass().getDeclaredFields(); for (Field field : fields) { try { field.setAccessible(true); Class<?> fieldType = field.getType(); String fieldName = field.getName(); Object fieldValue = field.get(orig); // skip "orig" object's field that it's wrapper type and null; // and "dest" object's field that it's primitive type should be the default value automatically when "orig" object's filed value null; if (ClassUtils.isPrimitiveWrapper(fieldType) && fieldValue == null) { continue; } BeanUtils.copyProperty(dest, fieldName, fieldValue); } catch (IllegalAccessException e) { logger.error(e); } catch (InvocationTargetException e) { logger.error(e); } } }
result test:
@Test public void test01() throws InvocationTargetException, IllegalAccessException { Product02 product02 = new Product02(); product02.setName("name02"); product02.setProduct_int(123); product02.setProduct_double(4.56); product02.setPrice(new Amount("product type1111", new BigDecimal(2681))); Product01 product01 = new Product01(); System.out.println(product02); copyProperties(product01, product02); //BeanUtils.copyProperties(product01, product02); System.out.println(product01); }
Product02{name='name02', product_int=123, product_double=4.56, product_boolean=null, product_char=null, product_byte=null, product_long=null, price=Amount{productTpye='product type1111', price=2681}}
Product01{name='name02', product_int=123, product_double=4.56, product_boolean=false, product_char= , product_byte=0, product_long=0, product_float=0.0, price=Amount{productTpye='product type1111', price=2681}}
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树