GeeCON Prague 2014*
The moose likes Java in General and the fly likes Reflection and Recursion, Pro�s and Con�s and any alternate strategy suggestions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection and Recursion, Pro�s and Con�s and any alternate strategy suggestions." Watch "Reflection and Recursion, Pro�s and Con�s and any alternate strategy suggestions." New topic
Author

Reflection and Recursion, Pro�s and Con�s and any alternate strategy suggestions.

Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

Hi,

Ok, I have landed myself into a tricky place and don�t now how to get out of it [nothing new in that]

In our application, we use XML extensively for communication with external systems while the data is maintained internally in the application as DTO�s [Data Transfer Objects] or DVO�s [Data Value Objects] or Java Beans with just getter/setter methods or whatever the terminology used.

The data travels in the form of java objects internal to the application, while when we need to publish the same externally we convert the same into XML as I guess is the standard.

The issue is, for conversion of the xml to java objects and vice versa we have an itsy bitsy utility class which traverses through the nodes and child nodes and gives a definite object structure to it. We use reflection in here for mapping the node name to java class which gets instantiated at runtime and the node traversal is written in a recursive loop.

Now the technical architect is sitting on my head and wants to do away with the twin R&R as he says they are costly processes.

I ran load tests and found that I/O for reading some properties [which again is done only once] is much more costly as compared to part of code which is being marked for being a performance hog as in the Reflection and Recursion stuff. I also visited sites which warn again going in for the Twin R�s and in fact reflection is considered a down right performance hog but I do not see that happening in the code.

We did the memory profiler and GC checks to map the memory consumptions and objects being created and they seemed all right.

PS: we also tested out utilities like castor, but they provide functionality way beyond our requirement and there are some issues on open/third party API�s so the code has to be ours.

PS1: The thing works, and it works beautifully as of now [or at least that is what I think]. My doubt is will it be a drag in production.

PS2: Sorry my question goes as do I really look for alternatives for the code as it uses stuff which is not considered the Ferraris, ok sorry Renault�s of java and is reflection really such a bad kid in the java family.

Thank you.
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

The code, some private methods such as initializeResourceBundle, getDocument etc are not included as they are not actually in scope of my issue.


objectRB = ResourceBundle which contains the fully qualified class name that needs to be invoked mapped to a nodename

methodRB = ResourceBundle which contains the method name which needs to be called to set the child object into the parent as in the object created from the child node into the object created from the parent node.






[ November 16, 2005: Message edited by: Devesh H Rao ]
[ November 16, 2005: Message edited by: Devesh H Rao ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I have very similar code that reads attributes from an LDAP directory and uses reflection to find corresponding setter methods in a Java object. Not all LDAP attributes correspond to properties of the object. This code is called once when a user connects to our web application.

Now, it's possible that this is a "costly process", but I haven't heard any complaints about how long it takes to sign on. We do get feedback about various problems but not about that.

If I were to remove reflection under pressure from a manager, I would replace it by a large nested if-statement which I'm sure you can picture for yourself. (Note that after that, if we add a new property to the Java object then we need to add a new branch to the nested if-statement. We didn't need to do that with the reflection solution.) But then I would be interested to find out just how much performance improvement resulted.
jiju ka
Ranch Hand

Joined: Oct 12, 2004
Posts: 306
We are also doing an XMl to bean mapping.

Two solution we considered is
Use a tool like betwixt - apache commons or XMLBeans
or
Write custom tool.

We considered using reflection and getting around without using reflection to create a custom tool.

Our beans are in the form setValue(propertyName:String, value bject) and getValue(propertyName:String). By this we can use getvalue and setValue instead of invoking methods using reflection. This is not a good solution either. Because it is less maintenance friendly.
Rajagopal Manohar
Ranch Hand

Joined: Nov 26, 2004
Posts: 183
I really like the approach taken by Jibx http://jibx.sourceforge.net/ no need for reflextions if you go that way

just my 2 cents

-Raj
Hemal Pandya
Greenhorn

Joined: Nov 10, 2005
Posts: 18
Originally posted by Devesh H Rao:
Hi,

Ok, I have landed myself into a tricky place and don�t now how to get out of it [nothing new in that]



The architect might have his/her own agenda. Try to find out what he wants to use as replacement and you might be able to figure out why he doesn't like the current implementation.

For the conversion, you will either need a document that describes the binding or you will have to use reflection. Depending on how often the conversion needs to be done, the relative cost of one-time identifying classes and methods need not be expensive.

As a third alternative, consider using reflection to code-generate classes that do the conversion.
Devesh H Rao
Ranch Hand

Joined: Feb 09, 2002
Posts: 687

Thank you for the replies.

I guess the motive behind the change is slightly political, which I won't go to in here.

I just wanted to get a view from a wider audience before I stack up for the long winter in the trench

Anyways making the change doesn�t make any sense to me coz

1. The schema for the xml itself runs into thousands of lines and we deal with a huge amount of data. The xml-object conversion and vice-versa has been divided to handle only those conversion which are required something on lines of JIT conversions [Just in Time] and they are divided into small chunks so that the hierarchy doesn�t run too deep.

2. Replacing reflection will make me code for specific classes and the �if else�code will run into pages which I kind of absolutely hate.

3. Performance is slightly better [milliseconds under load] if reflection is not used but it is a trade off with configurability of the application and reuse of code.
 
GeeCON Prague 2014
 
subject: Reflection and Recursion, Pro�s and Con�s and any alternate strategy suggestions.