Last active
August 29, 2015 14:17
-
-
Save ThomasLau/fbee7468e57d9952fe4a to your computer and use it in GitHub Desktop.
HashMap中的一段方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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