Java Reflection - Get All Methods In Hierarchy
Quick static utility to obtain all declared methods in the class hierarchy.
/**
* Gets an array of all methods in a class hierarchy walking up to parent classes
* @param objectClass the class
* @return the methods array
*/
public static Method[] getAllMethodsInHierarchy(Class<?> objectClass) {
Set<Method> allMethods = new HashSet<Method>();
Method[] declaredMethods = objectClass.getDeclaredMethods();
Method[] methods = objectClass.getMethods();
if (objectClass.getSuperclass() != null) {
Class<?> superClass = objectClass.getSuperclass();
Method[] superClassMethods = getAllMethodsInHierarchy(superClass);
allMethods.addAll(Arrays.asList(superClassMethods));
}
allMethods.addAll(Arrays.asList(declaredMethods));
allMethods.addAll(Arrays.asList(methods));
return allMethods.toArray(new Method[allMethods.size()]);
}
Written by Raúl Raja
Related protips
1 Response
awesome
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Reflection
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#