Program to demonstrate Usage of Reflection API for getting methods name and return type in Java
Output of the program :
package com.hubberspot.reflection; import java.lang.reflect.Method; class Customer { // private fields private String firstname; private String lastname; private String email; private static int counter; // public method public String setFirstname( int count ) { return this.firstname = "Jonty"; } // protected method protected String setLastname( String name ) { return lastname = "Magic"; } // private method private String getEmail( String email ) { return email = "Jonty@Magic.com"; } // default method void setEmail(String email) { this.email = email; } // static method public static int getCounter() { return counter; } public static void setCounter(int counter) { Customer.counter = counter; } } public class CustomerMethods { public static void main(String[] args) { // Create a test object. We can create // and apply reflection on any class // we want without knowing which methods // it has. We are taking Customer here. Customer customer = new Customer(); // Getting Class associated with Customer Class custClass = customer.getClass(); try { System.out.println("-----------------------------------"); // Using Method class we can use method of any // class we want. Here we are getting method // setFirstname by calling getMethod method of the // Class associated with Customer Method method = custClass.getMethod("setFirstname", new Class[] {int.class}); // getParameterTypes() returns an array of parameter for the method Class[] parameterType = method.getParameterTypes(); // getName() returns the name of the method System.out.println("Method Name : " + method.getName()); // getReturnType().getName() returns the name of return type of method System.out.println("Method Return Type : " + method.getReturnType().getName()); // parameterType[0].getName() return the parameter type name of first parameter System.out.println("Method Parameter Type : " + parameterType[0].getName()); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } System.out.println("-----------------------------------"); // getDeclaredMethods() returns an array of methods declared in // class itself Method[] methods = custClass.getDeclaredMethods(); System.out.println("No. Methods in Customer : " + methods.length); for (Method method : methods) { System.out.println("Method Name : " + method.getName()); System.out.println("Method Return Type : " + method.getReturnType().getName()); Class[] parameterTypes = method.getParameterTypes(); for (Class classCustomer : parameterTypes) { System.out.println("Method Parameter Type : " + classCustomer.getName()); } System.out.println("-----------------------------------"); } System.out.println("-----------------------------------"); // getMethods() returns an array of methods declared in class and // in superclasses too methods = custClass.getMethods(); System.out.println("No. Methods in Customer : " + methods.length); for (Method method : methods) { System.out.println("Method Name : " + method.getName()); System.out.println("Method Return Type : " + method.getReturnType().getName()); Class[] parameterTypes = method.getParameterTypes(); for (Class classCustomer : parameterTypes) { System.out.println("Method Parameter Type : " + classCustomer.getName()); } System.out.println("-----------------------------------"); } } }
Output of the program :