aspose file tools*
The moose likes Java in General and the fly likes Getting the object's value using Reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting the object Watch "Getting the object New topic
Author

Getting the object's value using Reflection

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Hi All,
I have a scenario as mentioned below.

I have many objects of different Types - say., Employee,User,Student which is not related to each other - List<Object>

In the process of generalization with the code available with me, iam trying to put the fieldName and fieldValue in a Map.

Steps :

1) Got the Field Name as follows.


2. Now i want to get the object's values for each fields and set in a map (with Key-Field Name and value - fieldValue)

Since i have the Object, iam not able to access the specific field value.
This is a showstopper to me.

Can anybody help me in getting the field value with the above mentioned inputs.

PS : I am using JDK1.5

Thanks.

Regards
Senthil Kumar Sekar
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36466
    
  15
You mean you have a poorly-designed List<Object>? If you work out how to separate the different objects by type, you will not need all this reflection. If you really can’t segregate those Objects by type, maybe you need a new class to encapsulate them. Or you can create an interface to encapsulate common behaviour and retro-fit your classes to implement that interface.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Have you considered using instanceof?

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Aaaaaaaahhhhhhh .....
i do not want to use instanceof operator.Since i have many classes which will also grow in future(which will be handled dynamically)

Is there any other way to achieve this?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36466
    
  15
Yesterday, I wrote:. . . poorly-designed . . .
I still think you may have to review the design of the whole application, I am afraid.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the object's value using Reflection
 
Similar Threads
Display HashMap objects in XML format?
how to convert a result set into a xml file
FBN: criteriaFind(String criteria) method; is this ok ?
Using reflection to get the value of a final static field
Need help on JTree