获取泛型参数的泛型类型,获取泛型参数,根据一个泛型比较器Com
分享于 点击 44456 次 点评:24
获取泛型参数的泛型类型,获取泛型参数,根据一个泛型比较器Com
根据一个泛型比较器Comparator<T>对象,获取允许比较对象的类型
/** * 获取可比较的元素类型 */private Class<?> getComparableTypeType(Comparator<T> comparator) { Type[] types = comparator.getClass().getGenericInterfaces(); for (Type type : types) { if (type instanceof ParameterizedType) { ParameterizedType ptype = (ParameterizedType) type; if (Comparator.class.equals(ptype.getRawType())) { Type cmpType = ptype.getActualTypeArguments()[0]; if (cmpType instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) cmpType).getRawType(); } else { return (Class<?>) ptype.getActualTypeArguments()[0]; } } } } return Object.class;}//该片段来自于http://byrx.net
用户点评