Monday, 27 May 2024

Retrieve the method details of a bean by using BeanInfo#getMethodDescriptors

Using BeanInfo#getMethodDescriptors method, we can get the method descriptors of a bean.

Example

BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();

 

Find the below working application.

 

Employee.java 
package com.sample.app.model;

public class Employee {
	private static int totalEmployees;

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

	public Integer getId() {
		return id;
	}

	public void setId(Integer 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;
	}

	public static int getTotalemployees() {
		return totalEmployees;
	}

	public static int getTotalEmployees() {
		return totalEmployees;
	}

	public static void setTotalEmployees(int totalEmployees) {
		Employee.totalEmployees = totalEmployees;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
	}

}

 

GetMethodsOfABean.java

package com.sample.app.introspector;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.lang.reflect.Method;

import com.sample.app.model.Employee;

public class GetMethodsOfABean {

	private static MethodDescriptor[] methodDescriptors(final Class<?> clazz) throws IntrospectionException {
		BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
		return beanInfo.getMethodDescriptors();
	}

	public static void main(String[] args) throws IntrospectionException {
		MethodDescriptor[] methodDescriptors = methodDescriptors(Employee.class);

		for (MethodDescriptor methodDescriptor : methodDescriptors) {

			Method method = methodDescriptor.getMethod();
			// Check if the method belongs to the specified class (Employee)
			if (!method.getDeclaringClass().equals(Employee.class)) {
				continue;
			}

			String name = method.getName();
			System.out.println("Method name : " + name);

			Class<?>[] parameterTypes = method.getParameterTypes();

			if (parameterTypes.length == 0) {
				continue;
			}

			System.out.println("  Paramters : ");
			for (Class<?> parameterType : parameterTypes) {
				System.out.println("    " + parameterType);
			}

		}

	}

}

 

Output

Method name : getName
Method name : setTotalEmployees
  Paramters : 
    int
Method name : setId
  Paramters : 
    class java.lang.Integer
Method name : getTotalEmployees
Method name : setAge
  Paramters : 
    int
Method name : setName
  Paramters : 
    class java.lang.String
Method name : getAge
Method name : getId
Method name : getTotalemployees
Method name : toString

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment