0

Let, say for instance i have a POJO class employee with three attributes

1.Name (String) 2.Location (String) 3.Date of Birth (Date)

then i fired a query into database which retrieve first row of table and populate this POJO with table data as follows:-

Name - john location - USA Date of Birth - 27/09/2014

To retrieve the values from this POJO i have to call getName(),getLocation() and getDOB().

But is there any method by which i can get all the values which is store in the POJO, in an Object type array without using getter method

for example:

Object[0] has the value "John".
Object[1] has the value "USA".
Object[2] has the value "27/09/2014".

(In my case, there are around 80 attributes in a class and number of these attributes increases because of client requirements and i am fetching each and every value by getter method and every time a single attribute is added i have to write a getter method in the code to fetch values. I basically want a more dynamic solution to this problem.)

Tairman
  • 45
  • 1
  • 6
  • You've tagged this with "reflection", which implies you already know that reflection is an option here. – Oliver Charlesworth Sep 27 '14 at 15:30
  • What is the goal of having the information as an object array? You would have to use `(String) array[1]` to get the location (and pray that the location is indeed at index 1 and is indeed a String), instead of `user.getLocation()`. What's the advantage? – JB Nizet Sep 27 '14 at 15:34
  • @OliverCharlesworth i've tried reflection, but problem is it only allows you get the definition of class like method name or attribute name. What i'm looking for is a method by which i can use values that are stored in the attributes. I guess reflection dosen't have this functionality. – Tairman Sep 27 '14 at 16:43
  • @user3756094: Of course it has that functionality! Read the tutorial: http://docs.oracle.com/javase/tutorial/reflect/member/index.html. – Oliver Charlesworth Sep 27 '14 at 17:11

2 Answers2

0

I think what you are looking for is called reflection.

i hope this link helps http://docs.oracle.com/javase/tutorial/reflect/index.html or this answer: Is it possible to use Java Reflection to print out attributes of the parent class?

Community
  • 1
  • 1
DormaPadre
  • 35
  • 1
  • 7
0

you can try this:

    String[] getObjectsPublicMethods(Object o)
{
    Class clazz = o.getClass();
    Method[] methods = clazz.getDeclaredMethods();
    String[] result = new String[methods.length];
    for (int i=0; i<methods.length; ++i)
    {
        try
        {
            result[i] = (String) methods[i].invoke(o, new Object[] {})
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (InvocationTargetException e)
        {

        }
    }
    return result;        
}

This method uses reflection to get the information you want, BUT it assumes that the getter methods are declared public in this class AND that all of the public methods return String.

Gilad
  • 833
  • 1
  • 8
  • 13