• 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

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

 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Happily living in the valley of the dried frogs with a few tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic