How do I list declared methods of a class using Reflection?
Reflection you can interrogate any class and find out all the methods that have been declared. When you call the getDeclaredMethods() method, it will return to you a Method[] array with the objects of methods that have been declared public, protected, private or default(package) in the class.
Getting All Methods for a given Class
Mammal dog = new Mammal(); Class dogClass = dog.getClass(); Method[] declaredMethods = dogClass.getDeclaredMethods();
Calling the getDeclaredMethods() method will return to you a Method[] array with the objects of methods that have been public in the class.
Iterating through all the Declared Methods
for (Method method : declaredMethods) { int mods = method.getModifiers(); System.out.println(" DECLARED METHODS are " + method.getName() + " mods=" + Modifier.toString(mods) + " retType=" + method.getReturnType() ); }
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; public class GetAllDeclaredMethodsExample { public static void main(String[] args) { Mammal dog = new Mammal(); Class dogClass = dog.getClass(); System.out.println("\nDetails for " + dogClass.getCanonicalName()); Method[] publicMethods = dogClass.getDeclaredMethods(); for (Method method : publicMethods) { int mods = method.getModifiers(); System.out.println(" DECLARED METHODS are " + method.getName() + " mods=" + Modifier.toString(mods) + " retType=" + method.getReturnType() ); } } }
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 DECLARED METHODS are toString mods=public retType=class java.lang.String DECLARED METHODS are getName mods=public retType=class java.lang.String DECLARED METHODS are setName mods=public retType=void DECLARED METHODS are sleep mods=public retType=void DECLARED METHODS are incCount mods=private static retType=void DECLARED METHODS are getMammalCount mods=public static retType=int DECLARED METHODS are privateMethod mods=private retType=void DECLARED METHODS are protectedMethod mods=protected retType=void DECLARED METHODS are eat mods=public retType=void DECLARED METHODS are walk mods=public retType=void DECLARED METHODS are isHair mods=public retType=boolean DECLARED METHODS are setHair mods=public 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)
- Different ways that I can get Class objects in Java using Reflection?
- How do I instantiate a class with a private constructor?
- How do I a list of all of the class's constructors?
- How do I list public methods of a class using Reflection?
- How do I list declared methods of a class using Reflection?
- How do I list all the methods of a class using Reflection?
- How do I get a list of class modifiers?
- Hacking Immutable class using Java Reflection
- Hacking the Immutable String class using Java Reflection
Leave a Reply