0

Hi all,

I have employee class which I have 100 variables with getters and setters.
now I am passing employee object to processing method. Now I want to read all values in processing method.

Should I want to use getter for all member? Is there any way to achieve this.

pls any one help me

2
Contributors
6
Replies
32
Views
2 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • You can use the `java.beans.Introspector` class to get the `BeanInfo` for your bean. Then use the `BeanInfo` to get the `PropertyDescriptors`. That gives you an array of all the properties, from which you can call `getReadMethod()` to get each of the bean's get methods. You can `invoke` those methods to … Read More

  • `pd.getReadMethod()` gives you a `Method` object that represents a "get" method for the Bean. You can call the Method object's `invoke` method to execute the "get" method it represents and return the result Read More

2

You can use the java.beans.Introspector class to get the BeanInfo for your bean. Then use the BeanInfo to get the PropertyDescriptors. That gives you an array of all the properties, from which you can call getReadMethod() to get each of the bean's get methods. You can invoke those methods to get all the property values.

0

Hi JamesCherrill,

Thanks for the reply
I have tried java.beans.Introspector with following code.In that I want to get the values of testBean object. How can I get that particular objects value. Could please help me.

    TestBean testBean = new TestBean();
    testBean.setId("01");
    testBean.setName("myname01");
    TestBean testBean1 = new TestBean();
    testBean1.setId("02");
    testBean1.setName("myname02");
    BeanInfo info = Introspector.getBeanInfo(TestBean.class);
    for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
        System.out.println(pd.getName());
        System.out.println(pd.getValue(pd.getName()));
        System.out.println(pd.getReadMethod());
    }
1

pd.getReadMethod() gives you a Method object that represents a "get" method for the Bean. You can call the Method object's invoke method to execute the "get" method it represents and return the result

Edited by JamesCherrill

0

Thanks for your help .
now its working with following code

pd.getReadMethod().invoke(testBean)

Edited by anand01: added code

0

Dear JamesCherrill,
In the above code pd.getName() gives the list of bean variables in sorted order. Do we have any option for the same order in the class.
Thanks in advance.

Edited by anand01: edit

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.