Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the object's value using Reflection

 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2894
46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered using instanceof?

 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yesterday, I wrote:. . . poorly-designed . . .
I still think you may have to review the design of the whole application, I am afraid.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic