In this example we shall explain with a simple example to get the methods of a class using the reflection and get the return type. 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 methods.
- Call getDeclaredMethods() method from the Class instance which returns the Method[] array which contains all the methods whether it is private, public or default.
- Then call the getReturnType() on Method object to get the return type.
Lets look at this example to list of methods and return types of java.lang.Math class:
package javabeat.net.reflection; import java.lang.reflect.Method; /** * Get Method Return Types using Reflection * * @author krishna * */ public class JavaBeatReflectionExample { public static void main(String[] args) throws Exception { Class<?> mathClass = java.lang.Math.class; Method[] mathMethods; mathMethods = mathClass.getDeclaredMethods(); for (int i = 0; i < mathMethods.length; i++) { System.out.println(mathMethods[i] + " :: return type :: " + mathMethods[i].getReturnType()); } } }
Output
public static int java.lang.Math.abs(int) :: return type :: int public static long java.lang.Math.abs(long) :: return type :: long public static float java.lang.Math.abs(float) :: return type :: float public static double java.lang.Math.abs(double) :: return type :: double public static double java.lang.Math.sin(double) :: return type :: double public static double java.lang.Math.cos(double) :: return type :: double public static double java.lang.Math.tan(double) :: return type :: double public static double java.lang.Math.atan2(double,double) :: return type :: double public static double java.lang.Math.sqrt(double) :: return type :: double public static double java.lang.Math.log(double) :: return type :: double public static double java.lang.Math.log10(double) :: return type :: double public static double java.lang.Math.pow(double,double) :: return type :: double public static double java.lang.Math.exp(double) :: return type :: double public static int java.lang.Math.min(int,int) :: return type :: int public static long java.lang.Math.min(long,long) :: return type :: long public static float java.lang.Math.min(float,float) :: return type :: float public static double java.lang.Math.min(double,double) :: return type :: double public static int java.lang.Math.max(int,int) :: return type :: int public static long java.lang.Math.max(long,long) :: return type :: long public static float java.lang.Math.max(float,float) :: return type :: float public static double java.lang.Math.max(double,double) :: return type :: double public static double java.lang.Math.scalb(double,int) :: return type :: double public static float java.lang.Math.scalb(float,int) :: return type :: float public static int java.lang.Math.getExponent(float) :: return type :: int public static int java.lang.Math.getExponent(double) :: return type :: int public static double java.lang.Math.signum(double) :: return type :: double public static float java.lang.Math.signum(float) :: return type :: float public static double java.lang.Math.asin(double) :: return type :: double public static double java.lang.Math.acos(double) :: return type :: double public static double java.lang.Math.atan(double) :: return type :: double public static double java.lang.Math.toRadians(double) :: return type :: double public static double java.lang.Math.toDegrees(double) :: return type :: double public static double java.lang.Math.cbrt(double) :: return type :: double public static double java.lang.Math.IEEEremainder(double,double) :: return type :: double public static double java.lang.Math.ceil(double) :: return type :: double public static double java.lang.Math.floor(double) :: return type :: double public static double java.lang.Math.rint(double) :: return type :: double public static int java.lang.Math.round(float) :: return type :: int public static long java.lang.Math.round(double) :: return type :: long private static synchronized void java.lang.Math.initRNG() :: return type :: void public static double java.lang.Math.random() :: return type :: double public static double java.lang.Math.ulp(double) :: return type :: double public static float java.lang.Math.ulp(float) :: return type :: float public static double java.lang.Math.sinh(double) :: return type :: double public static double java.lang.Math.cosh(double) :: return type :: double public static double java.lang.Math.tanh(double) :: return type :: double public static double java.lang.Math.hypot(double,double) :: return type :: double public static double java.lang.Math.expm1(double) :: return type :: double public static double java.lang.Math.log1p(double) :: return type :: double public static double java.lang.Math.copySign(double,double) :: return type :: double public static float java.lang.Math.copySign(float,float) :: return type :: float public static double java.lang.Math.nextAfter(double,double) :: return type :: double public static float java.lang.Math.nextAfter(float,double) :: return type :: float public static double java.lang.Math.nextUp(double) :: return type :: double public static float java.lang.Math.nextUp(float) :: return type :: float