I want to get all methods of a class, including public, protected, package and private methods, and including inherited methods.
Remember:
Class.getDeclaredMethods()
gets public, protected, package and private methods, but excludes inherited methods.Class.getMethods
gets inherited methods, but only the public ones.
Before Java 8 we could do something along the lines of:
Collection<Method> found = new ArrayList<Method>();
while (clazz != null) {
for (Method m1 : clazz.getDeclaredMethods()) {
boolean overridden = false;
for (Method m2 : found) {
if (m2.getName().equals(m1.getName())
&& Arrays.deepEquals(m1.getParameterTypes(), m2
.getParameterTypes())) {
overridden = true;
break;
}
}
if (!overridden) found.add(m1);
}
clazz = clazz.getSuperclass();
}
return found;
But now, if the class implements some interface with default methods which are not overridden by concrete superclasses, these methods will escape the above detection. Besides, there are now rules concerning default methods with the same name, and these rules must be taken into account as well.
Question: What is the current recommended way of getting all methods of a class:
The most common definition of "all" should be the methods that can be directly accessed inside an instance method of the class, without the use of super
or class names:
- Include public, protected, package and private methods declared in the class itself.
- Include protected methods of its superclasses.
- Include package methods of its superclasses of the same package.
- Include default methods of its interfaces (those not overridden/hidden, see here and here).
- Include static methods (class and superclasses) with the appropriate accessibility.
- Don't include private methods of superclasses.
- Don't include overridden methods.
- Don't include hidden methods (in special, don't include hidden static methods).
- Don't include synthetic/bridge methods.
- Don't include methods not allowed by Java, even if the JVM allows them.
So, the above definition fits the following signature when both boolean flags are false
:
public Collection<Method> getAllMethods(Class clazz,
boolean includeAllPackageAndPrivateMethodsOfSuperclasses,
boolean includeOverridenAndHidden)
The ideal, canonical answer, should allow for these boolean flags.
I was not able to compile Holger's answer in an Android environment since
MethodType
is added in API level 26 and Android Studio supports a subset of Java 8 Language Features. In addition to this, Holger's code contains to much lambdas and streams, I consider those as human unreadable. So I decided to write a more readable code that works in any Java environment. But it's not an ideal solution since I did not include the flags.Below snippets work same as if you called
getAllMethods(clazz, false, false)
Two of the components of a method declaration comprise the method signature: The method's name and the parameter types. The compiler does not consider return type when differentiating methods, so you can not declare two methods with the same signature even if they have a different return type. So
MethodSignature
class does not hold any reference to the return type of it's method.But when you invoke
getDeclaredMethods
orgetMethods
it is possible to get multiple declared methods with the same name and parameter types, but different return types. This means that the compiler created a synthetic method, called a bridge method. To solve this problem, invokemethod.isSynthetic()
on the method, if it returns true skip it. Since it is a synthetic method there will be a non synthetic one with the same signature but different return type.Even for the “Before Java 8” scenario, your code snippet isn’t correct. But collecting all methods isn’t a usual scenario though, as you normally need methods regarding a certain context, e.g. you might want to know which methods are accessible for a given context, which doesn’t include all methods, even if you consider non-
public
methods. If you really want all methods, you have to recall thatprivate
andstatic
methods are never overridden and package-private methods are only overridden when being declared within the samepackage
. So it’s not correct to filter every encountered method signature.What makes matters worse is that methods might get overridden with different modifiers. The latter can be solved by keeping the idea to start at the actual class and use
Class.getMethods()
to get allpublic
method includingdefault
methods and traverse the superclass hierarchy towardsjava.lang.Object
so the already encountered overrides have the least restrictive access modifiers.As a side note, nesting linear search loops is never a good idea. You’ll soon end up with a quadratic or worse complexity.
You may collect methods using the following method:
But as said, it might be the case that it isn’t suitable for whatever you want to do. You should ask yourself the following questions first:
public
andprotected
only)?class
/package
context?static
methods be included?Here is the revised method adapted to your more specific request: