This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes Generalize instantiation of a data class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generalize instantiation of a data class" Watch "Generalize instantiation of a data class" New topic
Author

Generalize instantiation of a data class

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
I have a requirement wherein I have several data classes (classes which just have members and their getter and setters) and I need to instantiate them using a map that is passed to it. The map has the name value pairs. I want to generalize this process instead of doing this specifically in the constructor of each data class.

One option is to define a parent class of all the data classes and do introspection in its constructor and use the hashmap for setting the values.

But is there an already existing standard way of doing it so that I do not end up reinventing the wheel.


Thanks and Regards
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi Satya Maheshwari,

I'm grossly confused after reading your post (or I'm not aware of anything like this )
Satya Maheshwari wrote:I have a requirement wherein I have several data classes (classes which just have members and their getter and setters)

Is it same as JavaBean?
Satya Maheshwari wrote:I need to instantiate them using a map that is passed to it

Passed to what? to class? What does Map contain? What is its key and value? (pardon me if I sound rude - I don't mean to )
Satya Maheshwari wrote:and use the hashmap for setting the values

So, is it that Map will contain values of specific fields? In that case, I guess reflection APIs might help.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi Anayonkar, thanks for replying!!

Basically I have several entity beans on top of corresponding DB tables which I mentioned as data classes in the post. Yes reflection is the way I would do it if there isn't any already existing pattern for doing it.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Satya Maheshwari wrote:I have a requirement wherein I have several data classes (classes which just have members and their getter and setters) and I need to instantiate them using a map that is passed to it. The map has the name value pairs. I want to generalize this process instead of doing this specifically in the constructor of each data class.

One option is to define a parent class of all the data classes and do introspection in its constructor and use the hashmap for setting the values.

But is there an already existing standard way of doing it so that I do not end up reinventing the wheel.

I'm not aware of any existing Java API that allows you to populate instance variables from a Map. My vote also goes to reflection. It should be quick and painless to write a generic method to do that. I'd image it looking like this:


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generalize instantiation of a data class