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 I hate recursion! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "I hate recursion!" Watch "I hate recursion!" New topic

I hate recursion!

Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
I have a situation at work where I need to introspect some beans that have an aggregate (has a) relationship. For example I might have a base bean container that holds a reference to a Collection of beans that may in turn hold other Collections or just simple text (String) properties. In all cases the getters for any of the beans will either return a java.util.Collection, java.lang.Object (for beans) or java.lang.String for simple properties. I have a class that will do the dirty work of introspection which will be constructed with the Class object of the root (top level) bean. The plan is to return a Collection of String objects based on a property (String) parameter of the form:

All properties that are Collections must have the syntax shown above (ie [(*)], [N1-N2], etc.) So, I want to walk thru all of RootBean.CollectionBean (which will all be InteriorBeans) and elements 1-6 of each InteriorBean.InteriorCollection and add SimpleBean.Property to the Collection that I will return. Ordering of elements are guaranteed (all the Collections are actually ArrayLists). I have all the parsing of the above String coded and tested and have no problem with the actual introspection and reflection required to get the values. I need some help on how to set up the recursion to walk the whole chain. Any help would be appreciated (that means especially you Yingst!)
Note the (*) should be just * to keep the UBB demon happy.
[ March 28, 2004: Message edited by: Michael Morris ]

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Oh sure, now you want my help.
This hasn't been tested or, well, compiled, but I'd think the basic structure could look something like this:

I used syntax from 1.5 just to keep you on your toes. Also I assume that the beans are hierarchical, and no beans contain references to other beans higher in the hierarchy. I.e. if A contains B which contains C, C can't contain A or B. Else you may get an infinite loop. And makeNewBaseStr is intentionally vague at this point, as I'm not sure I understand the format you want anyway, and that's (hopefully) secondary to setting up the basic recursion structure. The basic idea is to build up the string progressively, adding pertinent info about each inner bean as it's uncovered. You'll probably need to add some more parameters to makeNewBaseStr() in order to do this correctly.
Hope that helps. More likely I've misunderstood what you wanted to do in the first place, but maybe this is close enough to be of use.

"I'm not back." - Bill Harding, Twister
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Thanks Jim. Today we decided to take a somewhat simpler approach for the time being. Now we will only deal with a single list of beans of the same type. Unfortunately we have just moved to WebSphere 5.0 which means we are still using JDK 1.3 and J2EE 1.3 so all that neat Tiger stuff is strictly verboten. We use a tag library that was developed in house which is a knock-off of Struts (I really don't understand why we just don't use Struts ) which is not well documented. I discovered (most serendipitously I might add ) a method that can be overridden in an ActionController which made handling these beans much simpler. Kinda reminds of an old Bob Seger tune that went something like Searching for mysteries without any clues. :roll:
I agree. Here's the link:
subject: I hate recursion!
jQuery in Action, 3rd edition