java反射机制应用例子

    由于我们从jsp 前台传来参数做更新操作时,经常要传递无需更新的字段,导致<input type="hidden" /> 特别多,不易于维护。
    所以,可以将 BeanUitils.copyProperties 再封装一下。
    
    例如:其中 user 负责接收页面参数,userInDB从数据库再拿出来,将user 有值的属性复制过去(但数值型属性需要自己指定,如:salary)
    User userInDB=userService.findById(user.getId());
    copyProperties(user, userInDB, new String[]{}, new String[]{salary});
    

    /**
     * 使用内省机制实现对象属性复制,也可以使用反射机制
     *
     * @param source源对象
     * @param target目标对象
     * @param ignoreProperties
     *            忽略不需要复制的属性
     * @param numberProperties
     *            数值型参数(由于无值情况下不知道是否要复制)
     * @throws Exception
     */
    public static void copyProperties(Object source, Object target,
            String ignoreProperties[], String numberProperties[])
            throws Exception {

        // 获取源对象属性,目标对象属性(数组)----sds,desds
        PropertyDescriptor[] sourcePds = Introspector.getBeanInfo(
                source.getClass()).getPropertyDescriptors();
        PropertyDescriptor[] targetPds = Introspector.getBeanInfo(
                target.getClass()).getPropertyDescriptors();

        // 方便比较获取:将sds转换为map, 将忽略属性数组,数值型数组转换为列表
        Map<String, PropertyDescriptor> sourcePdMap = new HashMap<String, PropertyDescriptor>();
        for (PropertyDescriptor p : sourcePds) {
            sourcePdMap.put(p.getName(), p);
        }

        List<String> ignorePropertieList = ignoreProperties == null ? null
                : Arrays.asList(ignoreProperties);
        List<String> numberPropertieList = numberProperties == null ? null
                : Arrays.asList(numberProperties);

        // 循环目标对象属性
        for (PropertyDescriptor targetPd : targetPds) {

            // 此属性不存在写的方法,,此属性是否不存在于源对象,原属性不存在get方法,在忽略的数组中
            if (targetPd.getWriteMethod() == null
                    || !sourcePdMap.containsKey(targetPd.getName())
                    || sourcePdMap.get(targetPd.getName()).getReadMethod() == null
                    || (ignorePropertieList != null && ignorePropertieList
                            .contains(targetPd.getName()))) {
                continue;

            }

            // 获取源属性值--sdMap
            Method getter = sourcePdMap.get(targetPd.getName()).getReadMethod();
            Object sourceValue = getter.invoke(source, new Object[] {});

            // 数值型:int(0),double(0.0),long(0),Interge Double
            // Long(null),不存在于复制的数组中。
            if ((sourceValue instanceof Integer
                    || sourceValue instanceof Double || sourceValue instanceof Long)
                    && !numberPropertieList.contains(targetPd.getName())) {
                continue;

                // 非数值型:String date:,timeStamp,po: 不为null;集合:不为
                // 空且不为空集合;普通po:不为null;
            } else if (sourceValue == null
                    || (sourceValue instanceof Collection && ((Collection) sourceValue)
                            .size() == 0)) {
                continue;

                // 复制
            } else {
                Method writter = targetPd.getWriteMethod();
                writter.invoke(target, sourceValue);

            }

        }

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值