Get Methods using Reflection

In this example we shall explain with a simple example to get the methods of a class using the reflection. You can list the public and private methods of an object using the following steps:

  • Get the Class instance of the class which you want to list the methods. In this example get the Class instance of Math class.
  • Call getMethods() method from the Class instance which returns the Method[] array which contains the public fields.
  • Call getDeclaredMethods() method from the Class instance which returns the Method[] array which contains all the methods whether it is private, public or default.

Lets look at this example to list of methods of java.lang.Math class:

package javabeat.net.reflection;

import java.lang.reflect.Method;
/**
 * List Methods in a Class using Reflection API
 * @author krishna
 *
 */
public class JavaBeatReflectionExample {

	public static void main(String[] args) throws Exception {

		// Get the Class instance for Math
		Class<?> mathClass = java.lang.Math.class;

		//Declare Fields class
		Method[] refMethods;

		// List all the public fields in the Math class
		refMethods = mathClass.getMethods();
		for (int i = 0; i < refMethods.length; i++) {
			System.out.println("Public Field: " + refMethods[i]);
		}

		System.out.println();

		// List all the fields in the Math class
		refMethods = mathClass.getDeclaredMethods();
		for (int i = 0; i < refMethods.length; i++) {
			System.out.println("Field: " + refMethods[i]);
		}

	}
}

Output

Public Field: public static int java.lang.Math.abs(int)
Public Field: public static long java.lang.Math.abs(long)
Public Field: public static float java.lang.Math.abs(float)
Public Field: public static double java.lang.Math.abs(double)
Public Field: public static double java.lang.Math.sin(double)
Public Field: public static double java.lang.Math.cos(double)
Public Field: public static double java.lang.Math.tan(double)
Public Field: public static double java.lang.Math.atan2(double,double)
Public Field: public static double java.lang.Math.sqrt(double)
Public Field: public static double java.lang.Math.log(double)
Public Field: public static double java.lang.Math.log10(double)
Public Field: public static double java.lang.Math.pow(double,double)
Public Field: public static double java.lang.Math.exp(double)
Public Field: public static int java.lang.Math.min(int,int)
Public Field: public static long java.lang.Math.min(long,long)
Public Field: public static float java.lang.Math.min(float,float)
Public Field: public static double java.lang.Math.min(double,double)
Public Field: public static int java.lang.Math.max(int,int)
Public Field: public static long java.lang.Math.max(long,long)
Public Field: public static float java.lang.Math.max(float,float)
Public Field: public static double java.lang.Math.max(double,double)
Public Field: public static double java.lang.Math.scalb(double,int)
Public Field: public static float java.lang.Math.scalb(float,int)
Public Field: public static int java.lang.Math.getExponent(float)
Public Field: public static int java.lang.Math.getExponent(double)
Public Field: public static double java.lang.Math.signum(double)
Public Field: public static float java.lang.Math.signum(float)
Public Field: public static double java.lang.Math.asin(double)
Public Field: public static double java.lang.Math.acos(double)
Public Field: public static double java.lang.Math.atan(double)
Public Field: public static double java.lang.Math.toRadians(double)
Public Field: public static double java.lang.Math.toDegrees(double)
Public Field: public static double java.lang.Math.cbrt(double)
Public Field: public static double java.lang.Math.IEEEremainder(double,double)
Public Field: public static double java.lang.Math.ceil(double)
Public Field: public static double java.lang.Math.floor(double)
Public Field: public static double java.lang.Math.rint(double)
Public Field: public static int java.lang.Math.round(float)
Public Field: public static long java.lang.Math.round(double)
Public Field: public static double java.lang.Math.random()
Public Field: public static double java.lang.Math.ulp(double)
Public Field: public static float java.lang.Math.ulp(float)
Public Field: public static double java.lang.Math.sinh(double)
Public Field: public static double java.lang.Math.cosh(double)
Public Field: public static double java.lang.Math.tanh(double)
Public Field: public static double java.lang.Math.hypot(double,double)
Public Field: public static double java.lang.Math.expm1(double)
Public Field: public static double java.lang.Math.log1p(double)
Public Field: public static double java.lang.Math.copySign(double,double)
Public Field: public static float java.lang.Math.copySign(float,float)
Public Field: public static double java.lang.Math.nextAfter(double,double)
Public Field: public static float java.lang.Math.nextAfter(float,double)
Public Field: public static double java.lang.Math.nextUp(double)
Public Field: public static float java.lang.Math.nextUp(float)
Public Field: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
Public Field: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
Public Field: public final void java.lang.Object.wait() throws java.lang.InterruptedException
Public Field: public boolean java.lang.Object.equals(java.lang.Object)
Public Field: public java.lang.String java.lang.Object.toString()
Public Field: public native int java.lang.Object.hashCode()
Public Field: public final native java.lang.Class java.lang.Object.getClass()
Public Field: public final native void java.lang.Object.notify()
Public Field: public final native void java.lang.Object.notifyAll()

Field: public static int java.lang.Math.abs(int)
Field: public static long java.lang.Math.abs(long)
Field: public static float java.lang.Math.abs(float)
Field: public static double java.lang.Math.abs(double)
Field: public static double java.lang.Math.sin(double)
Field: public static double java.lang.Math.cos(double)
Field: public static double java.lang.Math.tan(double)
Field: public static double java.lang.Math.atan2(double,double)
Field: public static double java.lang.Math.sqrt(double)
Field: public static double java.lang.Math.log(double)
Field: public static double java.lang.Math.log10(double)
Field: public static double java.lang.Math.pow(double,double)
Field: public static double java.lang.Math.exp(double)
Field: public static int java.lang.Math.min(int,int)
Field: public static long java.lang.Math.min(long,long)
Field: public static float java.lang.Math.min(float,float)
Field: public static double java.lang.Math.min(double,double)
Field: public static int java.lang.Math.max(int,int)
Field: public static long java.lang.Math.max(long,long)
Field: public static float java.lang.Math.max(float,float)
Field: public static double java.lang.Math.max(double,double)
Field: public static double java.lang.Math.scalb(double,int)
Field: public static float java.lang.Math.scalb(float,int)
Field: public static int java.lang.Math.getExponent(float)
Field: public static int java.lang.Math.getExponent(double)
Field: public static double java.lang.Math.signum(double)
Field: public static float java.lang.Math.signum(float)
Field: public static double java.lang.Math.asin(double)
Field: public static double java.lang.Math.acos(double)
Field: public static double java.lang.Math.atan(double)
Field: public static double java.lang.Math.toRadians(double)
Field: public static double java.lang.Math.toDegrees(double)
Field: public static double java.lang.Math.cbrt(double)
Field: public static double java.lang.Math.IEEEremainder(double,double)
Field: public static double java.lang.Math.ceil(double)
Field: public static double java.lang.Math.floor(double)
Field: public static double java.lang.Math.rint(double)
Field: public static int java.lang.Math.round(float)
Field: public static long java.lang.Math.round(double)
Field: private static synchronized void java.lang.Math.initRNG()
Field: public static double java.lang.Math.random()
Field: public static double java.lang.Math.ulp(double)
Field: public static float java.lang.Math.ulp(float)
Field: public static double java.lang.Math.sinh(double)
Field: public static double java.lang.Math.cosh(double)
Field: public static double java.lang.Math.tanh(double)
Field: public static double java.lang.Math.hypot(double,double)
Field: public static double java.lang.Math.expm1(double)
Field: public static double java.lang.Math.log1p(double)
Field: public static double java.lang.Math.copySign(double,double)
Field: public static float java.lang.Math.copySign(float,float)
Field: public static double java.lang.Math.nextAfter(double,double)
Field: public static float java.lang.Math.nextAfter(float,double)
Field: public static double java.lang.Math.nextUp(double)
Field: public static float java.lang.Math.nextUp(float)

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*