예제 #1
0
    public Object resolve(Invocation inv, ParamMetaData metaData) {
      String toConvert = null;
      //
      FlashParam flashParam = metaData.getAnnotation(FlashParam.class);
      if (flashParam != null) {
        toConvert = inv.getFlash().get(flashParam.value());
      }

      for (String paramName : metaData.getParamNames()) {
        if (paramName != null) {
          toConvert = inv.getRequest().getParameter(paramName);
          if (toConvert != null) {
            break;
          }
        }
      }
      if (toConvert == null) {
        DefValue defValudeAnnotation = metaData.getAnnotation(DefValue.class);
        if (defValudeAnnotation != null
            && !DefValue.NATIVE_DEFAULT.equals(defValudeAnnotation.value())) {
          toConvert = defValudeAnnotation.value();
        }
      }
      if (toConvert != null) {
        SimpleTypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
        return typeConverter.convertIfNecessary(toConvert, metaData.getParamType());
      }
      if (metaData.getParamType().isPrimitive()) {
        // 对这最常用的类型做一下if-else判断,其他类型就简单使用converter来做吧
        if (metaData.getParamType() == int.class) {
          return Integer.valueOf(0);
        } else if (metaData.getParamType() == long.class) {
          return Long.valueOf(0);
        } else if (metaData.getParamType() == boolean.class) {
          return Boolean.FALSE;
        } else if (metaData.getParamType() == double.class) {
          return Double.valueOf(0);
        } else if (metaData.getParamType() == float.class) {
          return Float.valueOf(0);
        } else {
          SimpleTypeConverter typeConverter = SafedTypeConverterFactory.getCurrentConverter();
          return typeConverter.convertIfNecessary("0", metaData.getParamType());
        }
      }
      return null;
    }
예제 #2
0
 protected Date resolveUtilDate(String text, ParamMetaData metaData) throws ParseException {
   if (StringUtils.isEmpty(text)) {
     DefValue defaultValudeAnnotation = metaData.getAnnotation(DefValue.class);
     if (defaultValudeAnnotation != null
         && !DefValue.NATIVE_DEFAULT.equals(defaultValudeAnnotation.value())) {
       if (StringUtils.isEmpty(defaultValudeAnnotation.value())) {
         return new Date(); // 当前时间!
       } else {
         text = defaultValudeAnnotation.value(); // 改变要被解析的文本!
       }
     } else {
       return null; // 保留null,而非当前时间
     }
   }
   Annotation[] paramAnnotations =
       metaData.getMethod().getParameterAnnotations()[metaData.getIndex()];
   for (Annotation annotation : paramAnnotations) {
     if (annotation instanceof Pattern) {
       String[] patterns = Pattern.class.cast(annotation).value();
       for (String pattern : patterns) {
         // 以long为时间
         if ("long".equals(pattern)) {
           boolean digit = true;
           for (int i = 0; i < text.length(); i++) {
             if (!Character.isDigit(text.charAt(i))) {
               digit = false;
               break;
             }
           }
           if (digit) {
             return new Date(Long.parseLong(text));
           }
         }
         // 可以配置多个pattern!! 通过长度匹配
         if (text.length() == pattern.length()) {
           return new SimpleDateFormat(pattern).parse(text);
         }
       }
       break;
     }
   }
   return DatePatterns.parse(text);
 }