• 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 optimization

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a set of value object classes that have a set of attributes and a matching set of get/set methods. I want to accept a Hashtable into a method (in the super class of the value objects) that will loop through the hash or the methods and call the set methods using a hash key XXXX so that setXXXX( hash.getAttribute( XXXX ) ) is called.
This may be overkill, but it gives some measure of flexibility. Once use would be if you have a J2EE webapp (I do!) and name the form elements on a post properly, you can just take the hash of parameter names and automatically set your object attributes. JSPs do this automaticly with the 'setProperty="*"' thingy, and I wanted to do it in a plain data class. Anyway, can somebody review the code below and suggest optimizations or a better way of doing this? I tried looping through the keys the first time, but had trouble creating methods, so I looped through the methods array instead. Seems rather inefficient to me. Thanks!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gerry,
I haven't reviewed your code in detail but I know of a framework that includes utility classes that do this already: Jakarta Struts. Specifically, you can use RequestUtils.populate() or BeanUtils.populate() to do the job. Both classes are in the org.apache.struts.util package. RequestUtils.populate takes an Object and an HttpServletRequest while BeanUtils.populate takes an Object and a Map. Very handy, and it's already been tested.
It looks like you're going the same way with your code anyway. Your method looks like it would be part of the object that you want to "autopopulate". You would probably realize later on that you want to refactor it to a utility class instead (after you've copied and pasted the method to several different Value Objects). Also, the method takes a Hashtable (an implementation) and you would probably realize later that you should refactor it to take a Map (an interface) instead, the same way that BeanUtils.populate() does.
And then there's also the question of indexed properties, which the struts.util classes also handle.
Is this enough to get you past the NIH (not invented here) syndrome?
HTH,
Junilu
 
CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic