Last active
August 29, 2015 14:24
-
-
Save ryanpersaud/7c11e0ee00bb8fc53b50 to your computer and use it in GitHub Desktop.
clojure.lang.Reflector.getMethods()
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 final ConcurrentHashMap<String, List> getMethodsCache = new ConcurrentHashMap<String, List>(); | |
static public List getMethods(Class c, int arity, String name, boolean getStatics){ | |
// Build the cache key | |
String key = c.getName() + String.valueOf(arity) + name + String.valueOf(getStatics); | |
// Check the cache | |
ArrayList methods = getMethodsCache.get(); | |
if(methods != null) { | |
return methods; | |
} | |
Method[] allmethods = c.getMethods(); | |
methods = new ArrayList(); | |
ArrayList bridgeMethods = new ArrayList(); | |
for(int i = 0; i < allmethods.length; i++) | |
{ | |
Method method = allmethods[i]; | |
if(name.equals(method.getName()) | |
&& Modifier.isStatic(method.getModifiers()) == getStatics | |
&& method.getParameterTypes().length == arity) | |
{ | |
try | |
{ | |
if(method.isBridge() | |
&& c.getMethod(method.getName(), method.getParameterTypes()) | |
.equals(method)) | |
bridgeMethods.add(method); | |
else | |
methods.add(method); | |
} | |
catch(NoSuchMethodException e) | |
{ | |
} | |
} | |
// && (!method.isBridge() | |
// || (c == StringBuilder.class && | |
// c.getMethod(method.getName(), method.getParameterTypes()) | |
// .equals(method)))) | |
// { | |
// methods.add(allmethods[i]); | |
// } | |
} | |
if(methods.isEmpty()) | |
methods.addAll(bridgeMethods); | |
if(!getStatics && c.isInterface()) | |
{ | |
allmethods = Object.class.getMethods(); | |
for(int i = 0; i < allmethods.length; i++) | |
{ | |
if(name.equals(allmethods[i].getName()) | |
&& Modifier.isStatic(allmethods[i].getModifiers()) == getStatics | |
&& allmethods[i].getParameterTypes().length == arity) | |
{ | |
methods.add(allmethods[i]); | |
} | |
} | |
} | |
// Don't bother with locking, its ok if two or more threads run the full method one time each | |
getMethodsCache.putIfAbsent(key, methods); | |
return methods; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment