Java Reflection for Methods


Create entities package and create new class named Student as below:

package entities;

public class Student {

	private String id;
	private String name;
	private int age;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}




package reflection;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import entities.Student;

public class Main {

	private static void displayAllMethods(Student student) {
		Class studentClass = student.getClass();
		Method[] methods = studentClass.getMethods();
		System.out.println("Count Methods: " + methods.length);
		System.out.println("Methods List");
		for (Method method : methods) {
			System.out.println("Method Name: " + method.getName());
			System.out.println("Method Return Type: " + method.getReturnType().getName());
			System.out.println("Method Modifier: " + Modifier.toString(method.getModifiers()));
			System.out.println("Count Parameter: " + method.getParameterCount());
			if (method.getParameterCount() > 0) {
				for (Parameter parameter : method.getParameters()) {
					System.out.println("\t" + parameter.getName() + ": " + parameter.getType());
				}
			}
			System.out.println("=======================================");
		}
	}

	public static void main(String[] args) {

		Student student = new Student();
		displayAllMethods(student);

	}

}
Count Methods: 15
Methods List
Method Name: getName
Method Return Type: java.lang.String
Method Modifier: public
Count Parameter: 0
=======================================
Method Name: getId
Method Return Type: java.lang.String
Method Modifier: public
Count Parameter: 0
=======================================
Method Name: setName
Method Return Type: void
Method Modifier: public
Count Parameter: 1
	arg0: class java.lang.String
=======================================
Method Name: setId
Method Return Type: void
Method Modifier: public
Count Parameter: 1
	arg0: class java.lang.String
=======================================
Method Name: setAge
Method Return Type: void
Method Modifier: public
Count Parameter: 1
	arg0: int
=======================================
Method Name: getAge
Method Return Type: int
Method Modifier: public
Count Parameter: 0
=======================================
Method Name: wait
Method Return Type: void
Method Modifier: public final
Count Parameter: 0
=======================================
Method Name: wait
Method Return Type: void
Method Modifier: public final
Count Parameter: 2
	arg0: long
	arg1: int
=======================================
Method Name: wait
Method Return Type: void
Method Modifier: public final native
Count Parameter: 1
	arg0: long
=======================================
Method Name: equals
Method Return Type: boolean
Method Modifier: public
Count Parameter: 1
	arg0: class java.lang.Object
=======================================
Method Name: toString
Method Return Type: java.lang.String
Method Modifier: public
Count Parameter: 0
=======================================
Method Name: hashCode
Method Return Type: int
Method Modifier: public native
Count Parameter: 0
=======================================
Method Name: getClass
Method Return Type: java.lang.Class
Method Modifier: public final native
Count Parameter: 0
=======================================
Method Name: notify
Method Return Type: void
Method Modifier: public final native
Count Parameter: 0
=======================================
Method Name: notifyAll
Method Return Type: void
Method Modifier: public final native
Count Parameter: 0
=======================================