This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Dynamically populate java bean tree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Dynamically populate java bean tree" Watch "Dynamically populate java bean tree" New topic
Author

Dynamically populate java bean tree

Amar Jee
Greenhorn

Joined: Oct 22, 2010
Posts: 6
Hi,
I want to dynamically populate a java bean which has non primitive members, from a map.
By dynamically, I mean, I should be able to loop on the properties (as, the javabean type will be know at runtime) and set them based on key in the map.
I tried achieving the same using BeanUtilsBean.populate(), but it did not work.
For example, if the bean structure is as follows, I want to be able to populate Wrapper (its members) based on the map below the code snippet.

-------------------------
Key | Value
-------------------------
name | Tim
age | 29
faltNumber | 307
street | 78
city | NY
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

The API in the package java.beans can be used to access properties in Java beans. Start with class Introspector. It has getBeanInfo() methods which will give you a BeanInfo object, which has a method getPropertyDescriptors() that will give you a PropertyDescriptor for each of the bean's properties. The PropertyDescriptor contains methods to get the methods that you can call (via reflection) to read or write the bean property.

I have some code that allows you to view a Java Bean as a map. You could use it like this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
GeeCON Prague 2014
 
subject: Dynamically populate java bean tree