copvalue from wrapper type to primitive type
来源:互联网 发布:java 基础 代码 编辑:程序博客网 时间:2024/06/04 13:47
/** * <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:
@Testpublic 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:
@Testpublic 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}}
- copvalue from wrapper type to primitive type
- copvalue from wrapper type to primitive type
- copy wrapper tpye value to primitive type in simlar obejct except its fileds is primitive type
- attempted to return null from a method with a primitive return type (int).
- mybatis -----attempted to return null from a method with a primitive return type (int)
- MyBatis提示getFundBonus attempted to return null from a method with a primitive return type (double)
- attempted to return null from a method with a primitive return type (int)异常的解决
- Convert from type X to type Y
- 关于attempted to return null from a method with a primitive return type (int).报错解决方案
- Professional JS(5.5.5-function properties and methods&&5.6primitive wrapper type)
- hibernate assigned to a property of primitive type setter
- Primitive, referrence and Value Type
- Implicit Primitive Type Conversion Rule
- Convert from type X to type Y in JAVA
- 【java】Mybatis返回int类型为空时报错 attempted to return null from a method with a primitive return type (int)
- assigning to id from incompatible type
- aop.AopInvocationException: null return value from advice does not match primitive return type for
- Null value was assigned to a property of primitive type setter of
- 读写其他应用程序的sharedpreferences
- STM32学习经验(补充)
- Windows 2008 账户组及权限
- java数据结构(三)——队列
- zf框架下smatry模板tpl输出xls文件
- copvalue from wrapper type to primitive type
- Java继承和组合的区别
- 用thinkphp开发微信,接口配置总失败问题
- 如何把void* 类型转变为二维数组
- input搜索框的隐藏和显示
- hadoop中 hadoop fs -getmerge 命令合并多个文件
- 端口随意开很危险 常见端口解析
- 装配Bean
- android开发之dip,dp与px像素之间的转换工具,可能用的不多,但是有总比没有好吧。