HashMap 续集
https://blog.csdn.net/harvic880925/article/details/50085595 可参考
comparableClassFor:
/**
* Returns x's Class if it is of the form "class C implements
* Comparable<C>", else null.
* 如果继承Comparable 就返回他的class 如果不是就返回null
* 这个方法在putTreeVal的时候会用到 会把返回值传递给find方法 进行查询
*
*/
static Class<?> comparableClassFor(Object x) {
//x 是不是Comparable子类
if (x instanceof Comparable) {
//变量申明
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
//如果key是String类型
if ((c = x.getClass()) == String.class) // bypass checks
return c;
//获取普通类所继承的接口使用的是Class.getInterfaces()函数
// 获取泛型接口的对象 使用 getGenericInterfaces
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
//ParameterizedType代表一种泛型类型 public class Point<T> {} Point类就是泛型类型
//public class PointImpl extends Point<Integer> { }
//Class<?> clazz = PointImpl.class; Type type = clazz.getGenericSuperclass();
//获得PointImpl.class的父类,而它的父类是Point,这明显是一个泛型类型,所以它对应的类型就是ParameterizedType;
//getRawType获取泛型表达式类或者接口的Class对象 上边的例子泛型是Point 所以返回Point.Class
//这里是判断返回的是不是Comparable.class
//getActualTypeArguments 返回当前泛型表达式中,用来填充泛型变量的真正值的列表 上边例子就是Integer
if (((t = ts[i]) instanceof ParameterizedType) &&
((p = (ParameterizedType)t).getRawType() ==
Comparable.class) &&
(as = p.getActualTypeArguments()) != null &&
as.length == 1 && as[0] == c) // type arg is c
return c;
}
}
}
return null;
}