• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamic Typecasting

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requriement where All objects avaialbe in list.During method invocation I need to extract values from object.
Is there any way we typecast an object dynamically and find values of each member

ex:

If this is not possible with what other best design we can make our code generic so that It will work for all types of Objects(Pojos).


Regards,
Naresh
[ December 05, 2008: Message edited by: Nitesh Kant ]
 
Ranch Hand
Posts: 84
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Naresh,


If i understood you, i have two possible solutions the first it's using a visitor pattern to process the objects according to their type. Sometimes i don't like much this pattern personally and instead just for decoupling. I use a map<Class<?>,Interface>. With that map you could pass the class of the object and obtain a custom behavior according to it.

I hope this could help you,
 
Naresh gangishetty
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you be more celar on second approach.Can we extract values set for an object with out knowing its type.

User user =new User();
user.setName('naresh");
List list=new ArrayList();
list.add(user);
extract(list);


public void extract(List list)
{

Object obj=list.get(0);
//here i need value set for Name i.e "naresh".Same code should work for any other type of object ex:Customer


}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naresh gangishetty ,

Please ignore all the stuff about maps and visitors above -- it doesn't speak to your question at all.

Check out the reflection API, which involves the class java.lang.Class and the package java.lang.reflect. There's even a method named getFields() (it's in java.lang.Class). Using reflection, you can find out what fields and methods an object has, get and set the values of the fields and invoke the methods.

You can learn all about it in this part of the Java tutorial.
 
Naresh gangishetty
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest Friedman-Hill .

You are right.I got the result I need with getMthod(),invoke() methods in reflection API.

Regards,
Naresh
 
reply
    Bookmark Topic Watch Topic
  • New Topic