Skip to content

Instantly share code, notes, and snippets.

@ThomasLau
Last active August 29, 2015 14:17
Show Gist options
  • Save ThomasLau/fbee7468e57d9952fe4a to your computer and use it in GitHub Desktop.
Save ThomasLau/fbee7468e57d9952fe4a to your computer and use it in GitHub Desktop.
HashMap中的一段方法
static Class<?> comparableClassFor(Object x) {
if (x instanceof Comparable) {
Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
if ((c = x.getClass()) == String.class) // bypass checks
return c;
if ((ts = c.getGenericInterfaces()) != null) {
for (int i = 0; i < ts.length; ++i) {
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment