File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Dynamically populate java bean tree

Amar Jee

Joined: Oct 22, 2010
Posts: 6
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: 14808

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
I agree. Here's the link:
subject: Dynamically populate java bean tree