In my Java Reflection article, I have shown you how to retrieve a Method
Object using Class.getMethod(methodName, parameterTypes)
. Class
Object has other methods too for introspecting about methods. In this article, I will show you an example for each of those methods.
Class.getMethods() Example
A class may have private, protected, default and public methods. getMethods
returns all of the public methods inherited or declared in the form of an array.
In the example I am going to show, I have class A
extending class B. B
oth classes implement their own interface.
A:
package javarticles.javareflection; public class A extends B implements IA { public void doAImpl(int a, int b) { } private void doAPrivate() { } @Override public void doAInterface() { } protected void doAProtected() { } void doAPackage() { } }
B:
package javarticles.javareflection; public class B implements IB { public void doBImpl(int a, int b) { } private void doBPrivate() { } @Override public void doBInterface() { } protected void doBProtected() { } void doBPackage() { } }
In the below example, we first load the class and then call getMethods
on it. We traverse through each of the returned Method
object and print the class containing the method and the method name.
FindMethodsExample:
package javarticles.javareflection; import java.lang.reflect.Method; public class FindMethodsExample { public static void main(String[] args) throws Exception { Class<?> classA = Class.forName("javarticles.javareflection.A"); System.out.println("getMethods:"); Method[] allMethodsInA = classA.getMethods(); for (Method method : allMethodsInA) { System.out.println(method.getDeclaringClass() + "." + method.getName()); } } }
Output:
class javarticles.javareflection.A.doAImpl class javarticles.javareflection.A.doAInterface class javarticles.javareflection.B.doBImpl class javarticles.javareflection.B.doBInterface class java.lang.Object.wait class java.lang.Object.wait class java.lang.Object.wait class java.lang.Object.equals class java.lang.Object.toString class java.lang.Object.hashCode class java.lang.Object.getClass class java.lang.Object.notify class java.lang.Object.notifyAll
In the output we can see public methods of class A's
and the public methods of its inherited class B's
.
If you notice the output also has methods from Object
class, like, wait
, equals
, toString
etc. This is obvious as Object
is the root class in the inheritance hierarchy.
Class.getDeclaredMethods() Example
If you are interested only in the methods declared by the target class and not bothered about the inherited methods, you need to call getDeclaredMethods
.
It will return all the methods declared by the target class including private, protected and default scoped.
FindDeclaredMethodsExample:
package javarticles.javareflection; import java.lang.reflect.Method; public class FindDeclaredMethodsExample { public static void main(String[] args) throws Exception { // Find declared methods Class<?> classA = Class.forName("javarticles.javareflection.A"); System.out.println("getDeclaredMethods:"); Method[] methodsInA = classA.getDeclaredMethods(); for (Method method : methodsInA) { System.out.println(method.getDeclaringClass() + "." + method.getName()); } } }
Output:
getDeclaredMethods: class javarticles.javareflection.A.doAImpl class javarticles.javareflection.A.doAPrivate class javarticles.javareflection.A.doAInterface class javarticles.javareflection.A.doAProtected class javarticles.javareflection.A.doAPackage
How to find all the declared methods (target as well as inherited)
Since getDeclaredMethods
only returns us the methods of the target class and not the inherited classes, we need to call getDeclaredMethods
on each of the super class. Class.getSuperclass()
will return us the super class.
In the below example, we traverse through each super class and then call getDeclaredMethods
on the obtained class.
If it is interface
, we only need to call getMethods
as interface will only have public methods.
FindAllDeclaredMethodsExample:
package javarticles.javareflection; import java.lang.reflect.Method; public class FindAllDeclaredMethodsExample { public static void main(String[] args) throws Exception { // Find declared methods Class<?> classA = Class.forName("javarticles.javareflection.A"); System.out.println("All declared methods:"); Class<?> clazz = classA; while (clazz != null) { Method[] methods = (clazz.isInterface() ? clazz .getMethods() : clazz.getDeclaredMethods()); for (Method method : methods) { System.out.println(method.getDeclaringClass() + "." + method.getName()); } clazz = clazz.getSuperclass(); } } }
Output:
All declared methods: class javarticles.javareflection.A.doAImpl class javarticles.javareflection.A.doAPrivate class javarticles.javareflection.A.doAInterface class javarticles.javareflection.A.doAProtected class javarticles.javareflection.A.doAPackage class javarticles.javareflection.B.doBImpl class javarticles.javareflection.B.doBPrivate class javarticles.javareflection.B.doBInterface class javarticles.javareflection.B.doBProtected class javarticles.javareflection.B.doBPackage class java.lang.Object.finalize class java.lang.Object.wait class java.lang.Object.wait class java.lang.Object.wait class java.lang.Object.equals class java.lang.Object.toString class java.lang.Object.hashCode class java.lang.Object.getClass class java.lang.Object.clone class java.lang.Object.notify class java.lang.Object.notifyAll class java.lang.Object.registerNatives
Download Source Code
In this article, I have shown you the different flavors of retrieving Method
object.
You can download the source code here: javareflection.zip