/** * 获取一个类的某个一个泛型参数 * * @param klass 类 * @param index 参数下标 (从 0 开始) * @return 泛型参数类型 */ @SuppressWarnings("unchecked") public static <T> Class<T> getTypeParam(Class<?> klass, int index) { Type[] types = extractTypeParams(klass); if (index >= 0 && index < types.length) { Type t = types[index]; Class<T> tClass = (Class<T>) LE.asClass(t); if (tClass == null) throw LE.makeThrow("Type '%s' is not a Class", t.toString()); return tClass; } throw LE.makeThrow("Class type param out of range %d/%d", index, types.length); }
private static <T extends Map<Object, Object>> Map<Object, Object> createMap(Class<T> mapClass) { Map<Object, Object> map; try { map = mapClass.newInstance(); } catch (Exception e) { map = new HashMap<Object, Object>(); } if (!mapClass.isAssignableFrom(map.getClass())) { throw LE.makeThrow("Fail to create map [%s]", mapClass.getName()); } return map; }