Package org.nutz.castor

Examples of org.nutz.castor.FailToCastObjectException


   * @throws FailToCastObjectException
   */
  @SuppressWarnings({"unchecked", "rawtypes"})
  public static <T> T map2Object(Map<?, ?> src, Class<T> toType) throws FailToCastObjectException {
    if (null == toType)
      throw new FailToCastObjectException("target type is Null");
    // 类型相同
    if (toType == Map.class)
      return (T) src;
    // 也是一种 Map
    if (Map.class.isAssignableFrom(toType)) {
      Map map;
      try {
        map = (Map) toType.newInstance();
        map.putAll(src);
        return (T) map;
      }
      catch (Exception e) {
        throw new FailToCastObjectException("target type fail to born!", e);
      }

    }
    // 数组
    if (toType.isArray())
      return (T) Lang.collection2array(src.values(), toType.getComponentType());
    // List
    if (List.class == toType) {
      return (T) Lang.collection2list(src.values());
    }

    // POJO
    Mirror<T> mirror = Mirror.me(toType);
    T obj = mirror.born();
    for (Field field : mirror.getFields()) {
      if (src.containsKey(field.getName())) {
        Object v = src.get(field.getName());
        if (null == v)
          continue;

        Class<?> ft = field.getType();
        Object vv = null;
        // 集合
        if (v instanceof Collection) {
          Collection c = (Collection) v;
          // 集合到数组
          if (ft.isArray()) {
            vv = Lang.collection2array(c, ft.getComponentType());
          }
          // 集合到集合
          else {
            // 创建
            Collection newCol;
            Class eleType = Mirror.getGenericTypes(field, 0);
            if (ft == List.class) {
              newCol = new ArrayList(c.size());
            } else if (ft == Set.class) {
              newCol = new LinkedHashSet();
            } else {
              try {
                newCol = (Collection) ft.newInstance();
              }
              catch (Exception e) {
                throw Lang.wrapThrow(e);
              }
            }
            // 赋值
            for (Object ele : c) {
              newCol.add(Castors.me().castTo(ele, eleType));
            }
            vv = newCol;
          }
        }
        // Map
        else if (v instanceof Map) {
          // 创建
          final Map map;
          if (ft == Map.class) {
            map = new HashMap();
          } else {
            try {
              map = (Map) ft.newInstance();
            }
            catch (Exception e) {
              throw new FailToCastObjectException("target type fail to born!", e);
            }
          }
          // 赋值
          final Class<?> valType = Mirror.getGenericTypes(field, 1);
          each(v, new Each<Entry>() {
View Full Code Here


      throws FailToCastObjectException {
    try {
      return Pattern.compile(src);
    }
    catch (Exception e) {
      throw new FailToCastObjectException("Error regex: " + src, e);
    }
  }
View Full Code Here

      return c;
    try {
      return Class.forName(src, false, Thread.currentThread().getContextClassLoader());
    }
    catch (ClassNotFoundException e) {
      throw new FailToCastObjectException(format("String '%s' can not cast to Class<?>!", src));
    }
  }
View Full Code Here

                  .getWrapperClass()
                  .getConstructor(String.class)
                  .newInstance(src);
    }
    catch (Exception e) {
      throw new FailToCastObjectException(String.format"Fail to cast '%s' to <%s>",
                                src,
                                toType.getName()), e);
    }
  }
View Full Code Here

     * @throws FailToCastObjectException
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public static <T> T map2Object(Map<?, ?> src, Class<T> toType) throws FailToCastObjectException {
        if (null == toType)
            throw new FailToCastObjectException("target type is Null");
        // 类型相同
        if (toType == Map.class)
            return (T) src;
        // 也是一种 Map
        if (Map.class.isAssignableFrom(toType)) {
            Map map;
            try {
                map = (Map) toType.newInstance();
                map.putAll(src);
                return (T) map;
            }
            catch (Exception e) {
                throw new FailToCastObjectException("target type fail to born!", unwrapThrow(e));
            }

        }
        // 数组
        if (toType.isArray())
            return (T) Lang.collection2array(src.values(), toType.getComponentType());
        // List
        if (List.class == toType) {
            return (T) Lang.collection2list(src.values());
        }

        // POJO
        Mirror<T> mirror = Mirror.me(toType);
        T obj = mirror.born();
        for (Field field : mirror.getFields()) {
            if (src.containsKey(field.getName())) {
                Object v = src.get(field.getName());
                if (null == v)
                    continue;

                Class<?> ft = field.getType();
                Object vv = null;
                // 集合
                if (v instanceof Collection) {
                    Collection c = (Collection) v;
                    // 集合到数组
                    if (ft.isArray()) {
                        vv = Lang.collection2array(c, ft.getComponentType());
                    }
                    // 集合到集合
                    else {
                        // 创建
                        Collection newCol;
                        Class eleType = Mirror.getGenericTypes(field, 0);
                        if (ft == List.class) {
                            newCol = new ArrayList(c.size());
                        } else if (ft == Set.class) {
                            newCol = new LinkedHashSet();
                        } else {
                            try {
                                newCol = (Collection) ft.newInstance();
                            }
                            catch (Exception e) {
                                throw Lang.wrapThrow(e);
                            }
                        }
                        // 赋值
                        for (Object ele : c) {
                            newCol.add(Castors.me().castTo(ele, eleType));
                        }
                        vv = newCol;
                    }
                }
                // Map
                else if (v instanceof Map && Map.class.isAssignableFrom(ft)) {
                    // 创建
                    final Map map;
                    // Map 接口
                    if (ft == Map.class) {
                        map = new HashMap();
                    }
                    // 自己特殊的 Map
                    else {
                        try {
                            map = (Map) ft.newInstance();
                        }
                        catch (Exception e) {
                            throw new FailToCastObjectException("target type fail to born!", e);
                        }
                    }
                    // 赋值
                    final Class<?> valType = Mirror.getGenericTypes(field, 1);
                    each(v, new Each<Entry>() {
View Full Code Here

        }
        try {
            return new BigDecimal(src);
        }
        catch (Exception e) {
            throw new FailToCastObjectException(String.format(    "Fail to cast '%s' to <%s>",
                                                                src,
                                                                toType.getName()), Lang.unwrapThrow(e));
        }
    }
View Full Code Here

                    .getWrapperClass()
                    .getConstructor(String.class)
                    .newInstance(src);
        }
        catch (Exception e) {
            throw new FailToCastObjectException(String.format(    "Fail to cast '%s' to <%s>",
                    src,
                    toType.getName()), Lang.unwrapThrow(e));
        }
    }
View Full Code Here

    protected java.util.Date toDate(String src) {
        try {
            return Times.D(src);
        }
        catch (Throwable e) {
            throw new FailToCastObjectException(e,
                                                "'%s' to %s",
                                                src,
                                                java.util.Date.class.getName());
        }
    }
View Full Code Here

            throws FailToCastObjectException {
        try {
            return Pattern.compile(src);
        }
        catch (Exception e) {
            throw new FailToCastObjectException("Error regex: " + src, Lang.unwrapThrow(e));
        }
    }
View Full Code Here

            return c;
        try {
            return Class.forName(src);
        }
        catch (ClassNotFoundException e) {
            throw new FailToCastObjectException(format("String '%s' can not cast to Class<?>!", src));
        }
    }
View Full Code Here

TOP

Related Classes of org.nutz.castor.FailToCastObjectException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.