示例#1
0
 public <T> T toObject(Class<T> classOfT) {
   Mirror<T> mirror = Mirror.me(classOfT);
   T re = mirror.born();
   Entry current = head;
   while (current != null) {
     mirror.setValue(re, current.name, current.value);
     current = current.next;
   }
   return re;
 }
示例#2
0
  @SuppressWarnings("unchecked")
  public <T extends CommonManager> T copy() {
    String clazzName = this.getClass().getName().replace(ClassAgent.CLASSNAME_SUFFIX, "");

    try {
      Mirror<T> mirror = (Mirror<T>) Mirror.me(Class.forName(clazzName));
      return ClassUtil.copySameProperties(mirror.born(), this);
    } catch (ClassNotFoundException e) {
      log.error(e.getMessage(), e);
      return null;
    }
  }
示例#3
0
文件: Lang.java 项目: flymichael/nutz
  /**
   * 根据一个 Map,和给定的对象类型,创建一个新的 JAVA 对象
   *
   * @param src Map 对象
   * @param toType JAVA 对象类型
   * @return JAVA 对象
   * @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 && 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>() {
                public void invoke(int i, Entry en, int length) {
                  map.put(en.getKey(), Castors.me().castTo(en.getValue(), valType));
                }
              });
          vv = map;
        }
        // 强制转换
        else {
          vv = Castors.me().castTo(v, ft);
        }
        mirror.setValue(obj, field, vv);
      }
    }
    return obj;
  }