• Post Reply Bookmark Topic Watch Topic
  • New Topic

Break composite bean into its components recursively  RSS feed

 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • 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.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!