コード例 #1
0
ファイル: Mirror.java プロジェクト: jekey/nutz
    public Class<?>[] extract(Mirror<?> mirror) {
      Class<?> theType = mirror.getType();
      List<Class<?>> re = new ArrayList<Class<?>>(5);

      // 原生类型,增加其外覆类
      if (theType.isPrimitive()) {
        re.add(mirror.getWrapperClass());
        // 数字
        if (theType != boolean.class && theType != char.class) {
          re.add(Number.class);
        }
      }
      // 日历
      else if (mirror.isOf(Calendar.class)) {
        re.add(Calendar.class);
      }
      // 其他类型,直接增加,并试图判断其抽象类
      else {
        re.add(theType);
        // 枚举
        if (mirror.klass.isEnum()) {
          re.add(Enum.class);
        }
        // 数组
        else if (mirror.klass.isArray()) {
          re.add(Array.class);
        }
        // 字符串
        else if (mirror.isStringLike()) re.add(CharSequence.class);
        // 数字
        else if (mirror.isNumber()) {
          re.add(Number.class);
        }
        // Map
        else if (mirror.isOf(Map.class)) {
          re.add(Map.class);
        }
        // 列表
        else if (mirror.isOf(List.class)) {
          re.add(List.class);
          re.add(Collection.class);
        }
        // 集合
        else if (mirror.isOf(Collection.class)) {
          re.add(Collection.class);
        }
      }
      // 最后确保 Object 一定被加上了
      if (theType != Object.class) re.add(Object.class);

      return re.toArray(new Class<?>[re.size()]);
    }
コード例 #2
0
ファイル: Lang.java プロジェクト: flymichael/nutz
 /**
  * 当一个类使用<T,K>来定义泛型时,本方法返回类的一个字段的具体类型。
  *
  * @param me
  * @param field
  * @return
  */
 public static Type getFieldType(Mirror<?> me, Field field) {
   Type type = field.getGenericType();
   Type[] types = me.getGenericsTypes();
   if (type instanceof TypeVariable && types != null && types.length > 0) {
     Type[] tvs = me.getType().getTypeParameters();
     for (int i = 0; i < tvs.length; i++) {
       if (type.equals(tvs[i])) {
         type = me.getGenericsType(i);
         break;
       }
     }
   }
   return type;
 }