Pin It

How to determine information about class methods at runtime using Java Reflection API ?.

Program to demonstrate how to determine information about class methods at runtime using Java Reflection API.

package com.hubberspot.reflection;

import java.lang.reflect.Method;

// Create a dummy class having methods which are 
// private, protected and public. It should have
// a return type and few parameters.

class Cricket {

 private int runs;
 private String playerName;


 public void setRun(int runs) {

 }

 protected int getRun(int rate) {
  return rate*runs;
 }

 private void playerName( String name ) {
  playerName = name;
 }

 public void playerRuns(int rate , int runs , String playerName) {

 }

}

public class MethodFindings {

 public static void main(String[] args) {

  // Creating a instance of class Class of type Cricket
  Class cricketClass = Cricket.class;

  // object of class Instance has a method by name 
  // getMethods() which returns back a list of
  // public methods in the form of array
  // The array type is of Method class
  Method[] methodsArr = cricketClass.getMethods();

  for (int j = 0; j < methodsArr.length; j++) {

   // Method class has method by name getName()
   // which returns back the name of the method
   String methodName = methodsArr[j].getName();

   System.out.println("Name of the method : " + methodName);

   // Method class also has a method by name getReturnTypes()
   // which returns a Class object that represents the formal 
   // return type of the method represented by Method object. 
   String returnType =
     methodsArr[j].getReturnType().getName();

   System.out.println("Return Type of the method : " + returnType);

   // Method class also has a method called as getParameterTypes(). 
   // Returns an array of Class objects of parameter types, 
   // in declaration order. Returns an array of length 0 if 
   // the underlying method takes no parameters.

   Class[] parameterTypes =
     methodsArr[j].getParameterTypes();

   System.out.print("Parameter Types in the method :");

   for (int k = 0; k < parameterTypes.length; k ++) {

    String paramTypeName = parameterTypes[k].getName();

    System.out.print(" " + paramTypeName);
   }
   System.out.println();
  }

  System.out.println("-------------------------------------------");
  System.out.println("For methods that are declared private and protected etc... ");

  // Class object also has a method by name getDeclaredMethods().
  // Returns an array of Method objects reflecting all the methods 
  // declared by the class. This includes public, protected, 
  // default access and private methods, but excludes inherited methods. 

  // Rest of the code is same as above ..... 

  methodsArr = cricketClass.getDeclaredMethods();

  for (int j = 0; j < methodsArr.length; j++) {

   String methodName = methodsArr[j].getName();

   System.out.println("Name of the method : " + methodName);

   String returnType =
     methodsArr[j].getReturnType().getName();

   System.out.println("Return Type of the method : " + returnType);

   Class[] parameterTypes =
     methodsArr[j].getParameterTypes();

   System.out.print("Parameter Types in the method :");

   for (int k = 0; k < parameterTypes.length; k ++) {

    String paramTypeName = parameterTypes[k].getName();

    System.out.print(" " + paramTypeName);
   }
   System.out.println();
  }

 }

}


Output of the program : 



 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot