关于根据方法名称调用方法的反射的实现的集中方案

 

     项目中将客户的信息保存到本地的Cookie中的使用的:由于每次查询时可以从Cookie中获取,如果改变必须保存到Cookie中,同时Cookie中的值仅仅为字符串形式保存在本地磁盘使用所以必须确保值为字符串格式。同时实时转换。

 

/**
  * 从Cookie获取的对象
  *
  * @param request
  */
 @SuppressWarnings("unchecked")
 public static VacationOrderConditionConvertor getFieldProperty2VacationOrderConditionConvertor(HttpServletRequest request) {
  List<FieldProperty> propertys = getClientVacationSetting(request);


  if (CollectionUtils.isNotEmpty(propertys)) {


   VacationOrderConditionConvertor convertor = new VacationOrderConditionConvertor();


   for (FieldProperty fieldProperty : propertys) {


    String propertyName = fieldProperty.getPropertyName();
    String propertyValue = fieldProperty.getPropertyValue();


    if (StringUtils.isBlank(propertyValue)) {
     continue;
    }


    try {

    //获取方法名称
     String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);

 


     System.out.println("method =" + methodName);

    //采用Spring的中ReflectionUtils获取方法并调用

     if (fieldProperty.getClazz() == String.class) {
 

      Method method = ReflectionUtils.findMethod(convertor.getClass(), methodName, new Class[] { String.class });
      method.invoke(convertor, methodName, String.valueOf(propertyValue));

  //采用Apache Commons BeanUtils中的 MethodUtils获取方法并调用

     } else if (fieldProperty.getClazz() == Long.class) {
      MethodUtils.invokeMethod(convertor, methodName, new Object[] { Long.valueOf(propertyValue) });

 

  //采用原始的反射代码s获取方法并调用

     } else if (fieldProperty.getClazz() == Enum.class) {
      Method method = ReflectionUtils.findMethod(VacationOrderConditionConvertor.class, methodName, new Class[] { Enum.class });
      method.invoke(convertor, Enum.valueOf(fieldProperty.getClazz(), propertyValue));


     }

//采用Spring的中ReflectionUtils反射处理异常的信息
    } catch (InvocationTargetException e) {
     ReflectionUtils.handleReflectionException(e);
    } catch (NoSuchMethodException e) {
     ReflectionUtils.handleReflectionException(e);
    } catch (IllegalAccessException e) {
     ReflectionUtils.handleReflectionException(e);
    }
   }
  }
  return null;
 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值