Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generalize instantiation of a data class

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic