Пример #1
0
 protected T blankInstance() {
   try {
     return backedType.newInstance();
   } catch (Exception e) {
     throw new RuntimeException(
         "Cannot instantiate " + backedType + ". It must have a default constructor", e);
   }
 }
Пример #2
0
  private static <T> T mapToClazz(Map<String, String> map, Class<T> clazz, String formatStr)
      throws InstantiationException, IllegalAccessException, InvocationTargetException {
    Map<String, String> mapdata = getCaseInsensitveMap(map);
    T paypalClassicResponseModel;
    paypalClassicResponseModel = clazz.newInstance();
    for (Field field : FieldUtils.getAllFields(clazz)) {
      if (String.class.isAssignableFrom(field.getType())) {
        BeanUtils.setProperty(
            paypalClassicResponseModel,
            field.getName(),
            mapdata.get(getFormatedKeyName(formatStr, field.getName(), null)));
      }

      if (List.class.isAssignableFrom(field.getType())) {
        PaypalCollection paypalCollection = field.getAnnotation(PaypalCollection.class);
        ParameterizedType ty = ((ParameterizedType) field.getGenericType());
        if (ty.getActualTypeArguments()[0] instanceof Class) {
          Class subClazz = (Class<?>) ty.getActualTypeArguments()[0];
          if (String.class.isAssignableFrom(subClazz)) {
            if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) {
              List<String> values =
                  fetchPartialKey(
                      mapdata,
                      getFormatedKeyName(paypalCollection.format(), field.getName(), null));
              BeanUtils.setProperty(paypalClassicResponseModel, field.getName(), values);
            } else {
              List<String> values = fetchPartialKey(mapdata, field.getName().toUpperCase());
              BeanUtils.setProperty(paypalClassicResponseModel, field.getName(), values);
            }
          }
          if (PaypalClassicModel.class.isAssignableFrom(subClazz)) {
            if (paypalCollection != null && StringUtils.isNotEmpty(paypalCollection.format())) {
              BeanUtils.setProperty(
                  paypalClassicResponseModel,
                  field.getName(),
                  mapToClazzList(mapdata, subClazz, paypalCollection.format()));
            }
          }
        }
      }
      if (PaypalClassicModel.class.isAssignableFrom(field.getType())) {
        BeanUtils.setProperty(
            paypalClassicResponseModel, field.getName(), mapToClazz(mapdata, field.getType()));
      }
    }
    return paypalClassicResponseModel;
  }