How do I list all methods of a class using Reflection?

In order to get a full list of all the methods available to you using Java Reflection you will need to call both the getMethods() and getDeclaredMethods() methods. This will return all the declared methods of a class which will include all private, protected, default(package) and public methods, and call the getMethods() method which will return public methods of the class or interface and all its inherited superclasses and superinterfaces.

Getting All Methods for a given Class and its Inherited Methods

HashSet<String> set= new HashSet<String>();
Mammal dog = new Mammal();
Class dogClass  = dog.getClass();
Method[] publicMethods = dogClass.getMethods();
Method[] declaredMethods = dogClass.getDeclaredMethods();

Calling the getMethods() method will return to you a Method[] array with the objects of methods that have been public in the class or interface and all its inherited superclasses and superinterfaces. Calling the getDeclaredMethods() will return all the declared methods of a class which will include all private, protected, default(package) and public methods (which, at this point are redundant)… I will iterate through each call and add to the HashSet each of the methods being returned, which we will use at the end for iterating the complete set back out to the console.

Iterating through all the Methods

for (Method method : publicMethods) {
  int mods = method.getModifiers();
   
  set.add(method.getName() 
       + " mods=" + Modifier.toString(mods)
       + " retType=" + method.getReturnType()
      );
}
for (Method method : declaredMethods) {
  int mods = method.getModifiers();
   
  set.add(method.getName() 
       + " mods=" + Modifier.toString(mods)
       + " retType=" + method.getReturnType()
      );
  // Now return the complete list of all methods available to us
  for(String s: set) {
      System.out.println("    ALL METHODS are " + s);
    }
}

Full Program Listing

package com.avaldes.tutorials;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashSet;

public class GetAllMethodsExample {

  public static void main(String[] args) {
    HashSet<String> set = new HashSet<String>();
    Mammal dog = new Mammal();
    Class dogClass = dog.getClass();
  
    System.out.println("\nDetails for " + dogClass.getCanonicalName());
    Method[] publicMethods = dogClass.getMethods();
    Method[] declaredMethods = dogClass.getDeclaredMethods();
    for (Method method : publicMethods) {
      int mods = method.getModifiers();
      
      set.add(method.getName() 
          + " mods=" + Modifier.toString(mods)
          + " retType=" + method.getReturnType()
          );
      
    }
    for (Method method : declaredMethods) {
      int mods = method.getModifiers();
      
      set.add(method.getName() 
          + " mods=" + Modifier.toString(mods)
          + " retType=" + method.getReturnType()
          );
      
    }

    // Now return the complete list of all methods available to us
    for (String s : set) {
      System.out.println("    ALL METHODS are " + s);
    }
  }
}

Full Program Listing (Mammal.java)

package com.avaldes.tutorials;

public class Mammal implements Animal {

  private boolean hair = false;
  private String name = null;
  private static int MammalCount = 0; 
  
  public Mammal() {
    incCount();
  }
  
  public String getName() {
    return name;
  }
  
  public static int getMammalCount() {
    return MammalCount;
  }


  private static void incCount() {
    MammalCount++;
  }

  public void setName(String name) {
    this.name = name;
  }

  private void privateMethod() {
    System.out.println("Private Method!!!");
  }
  
  protected void protectedMethod() {
    System.out.println("Protected Method!!!");
  }
  
  @Override
  public void eat() {
    System.out.println("Mammal is eating...");
  }

  @Override
  public void sleep() {
    System.out.println("Mammal is Sleeping...");
  }

  @Override
  public void walk() {
    System.out.println("Mammal is Walking...");
  }

  public boolean isHair() {
    return hair;
  }

  public void setHair(boolean hair) {
    this.hair = hair;
  }

  public String toString() {
    return "[name=" + getName() + ", hair=" + isHair() 
                    + ", Mammals=" + getMammalCount() + "]";
  }
}

Output

Details for com.avaldes.tutorials.Mammal
    ALL METHODS are protectedMethod mods=protected retType=void
    ALL METHODS are getMammalCount mods=public static retType=int
    ALL METHODS are setHair mods=public retType=void
    ALL METHODS are wait mods=public final native retType=void
    ALL METHODS are toString mods=public retType=class java.lang.String
    ALL METHODS are eat mods=public retType=void
    ALL METHODS are isHair mods=public retType=boolean
    ALL METHODS are getName mods=public retType=class java.lang.String
    ALL METHODS are hashCode mods=public native retType=int
    ALL METHODS are notifyAll mods=public final native retType=void
    ALL METHODS are equals mods=public retType=boolean
    ALL METHODS are getClass mods=public final native retType=class java.lang.Class
    ALL METHODS are setName mods=public retType=void
    ALL METHODS are sleep mods=public retType=void
    ALL METHODS are notify mods=public final native retType=void
    ALL METHODS are wait mods=public final retType=void
    ALL METHODS are incCount mods=private static retType=void
    ALL METHODS are walk mods=public retType=void
    ALL METHODS are privateMethod mods=private retType=void

Java Reflection Examples

  • Class Example
    Simple example shows you how to dynamically load and instantiate a class, get class name, package name, and superclass name.
  • Modifier Example
    Showing you how to get information about classes, fields and methods using modifier method.
  • Method Example
    Providing full details of how to get all methods, and invoke methods using reflection.

Frequently Asked Questions on Java Reflection API (FAQs)

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *