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

Recommended Answers

All 6 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 get all the property values.

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());
    }

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

Thanks for your help .
now its working with following code

pd.getReadMethod().invoke(testBean)

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.

Do you mean the order in which they are declared in the source code? No, I don't know any way to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.