This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Dynamically populate java bean tree

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
I'd appreciate it if you pronounced my name correctly. Pinhead, with a silent "H". Petite ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic