• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Break composite bean into its components recursively

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone share with me any code that recursively checks a composite domain object and breaks it up into granular domain objects? I am sure that someone else had to write code for doing so. Any interest in sharing some of that knowledge for re-use purposes? Is there any open source utility available for doing so?

Following is the scenario:



Composite bean Person has list of Address and Name as the beans. Address in turn has Street bean.

I would like to have a utility method that would return the list of all the beans for Person as follows:

List lstOfBeans = Util.getListOfBeans(instanceOfPerson)

lstOfBeans will be populated with an instance of Person, Address from the list and Street. If any member variable in the bean is qualified under package poc (e.g., poc.Person, poc.Address, poc.Name, poc.Street), then I will know that I have encountered a bean that need to be a part of the list.

Note that if the member variable were a List (e.g. List lstOfAddress, meaning a single person can have many addresses), I would like to have multiple entries from that lstOfAddress.

If the bean is not a composite bean, then Util.getListOfBeans(instanceOfDomainObject) will only return a single bean.

My questions:

1. Will anyone be interested in sharing the logic that you have used before for similar scenario?
2. Is there any open source utility code avaibale for doing so? I can use upto JDK 1.5 level.

Thanks in advance.

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't this just standard reflection with some checks for particular package names? Maybe check out Commons BeanUtils; it's slightly more convenient.
 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, agree, that java.lang.reflect, or BeanUtils would be of use.

But before I start coding, wanted to see if anybody would be willing to share Java code to recurse through a composite bean to get all component beans as a list. Code could be as:

public static List getComponents(ComponentBean bean){
//////TO DO: implement
}

poc.ComponentBean "has a" poc.TigerBean
poc.TigerBean in turn "has a" poc.ForestBean
poc.ComponentBean extends poc.ParentBean
poc.ParentBean "has a" GuitarBean with public getter method.

As such, getComponents(myComponentBean) would return a List that would contain all the ablove beans namely:

poc.ComponentBean
poc.TigerBean
poc.ForestBean
poc.ParentBean
poc.GuitarBean

If myComponentBean did not have any member variable starting with poc, then only populate the List with single bean, which is myComponentBean.

Obiously, java.lang.reflect and/or BeanUtils from Apache could be used. I could check all the beans starting with poc and could write recursive code to populate the list.

But before I start coding it, wanted to see if anybody would be willing to share any similar code written in the past.

Any version of Java would suffice.

Regards
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic