I would like to write, for example
Method[] getMethods(Class<?> c)
which will do the same as existing
Class.getMethods()
but also include private and protected methods. Any ideas how I could do this?
public Method[] getMethods(Class<?> c) { List<Method> methods = new ArrayList<Method>(); while (c != Object.class) { methods.addAll(Arrays.asList(c.getDeclaredMethods())); c = c.getSuperclass(); } return methods.toArray(new Method[methods.size()]); }
Explain:
getDeclaredMethods
c.getSuperclass()
Object
while (c != null)
Use Class.getDeclaredMethods() . Note that unlike getMethods() , this will not return the inherited methods - therefore, if you want everything, you will need to restore the type hierarchy.
Class.getDeclaredMethods()
getMethods()
The Javadoc documentation describes all the details.