• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Refactoring Reflection (any suggestions?)

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on an application that does a String to method call mapping. ie. A String parameter("doMethod") is passed into the application and a method( doMethod() ) is called based on that parameter. Currently we do this via reflection and we want to refactor out the reflection. Any suggetsions?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not a clue. If I were you I would get a copy of Martin Fowlers book on Refactoring and look it up (my copy is at home, or I would look for you).
Here is the bunkhouse review of it: http://www.javaranch.com/bunkhouse/bunkhouse_Design.jsp
Here is the link to the amazon site for it: http://www.amazon.com/exec/obidos/ASIN/0201485672/electricporkchop/103-8030986-9727857
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what you want to do is a callback function as it is called in C++.
I've not do anything like this in Java yet, even I know it well in C++. Reflection is definitely one way to do it. However, I've heard "Core Java" book talked about it. It is not in my hand now. Just give you something to look for.
Thanks!
Roseanne
Join our SCJD Study Group when certified

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to implement a callback function then use the Command Pattern:
http://www.javaworld.com/javaworld/javatips/jw-javatip68-2.html
Let me know if this helps,
Joe
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's not a silly question, why do you want to refactor out the reflection? In what way is it a bad solution to your problem?
 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are several of the forces that are pushing us in the direction of refactoring out the reflection.
(not in order of importance)
* While debugging, the stack trace is practically useless. Methods that we call via reflection all have basically the same stack trace.
* Performance issues. (Please correct me if I'm wrong, but reflection is rather cumbersome when it comes to performance, no?)
* The piece using reflection is inellegant. Adding new methods to be called via our reflection methods is somewhat cumbersome.
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Landon,
I agree that reflection is an inelegant, and not very O-O, solution to most problems. You could use a large switch statement to select the method to call, but that's hardly more elegant. Without seeing any of your design, I can only make a vague suggestion. You might use polymorphism by creating several subclasses of a single base. They'd all have the same method but different implementations - you could select which one to instantiate based on the argument, and the code that calls the method could do so through a reference to their base class.
If that doesn't help, post back some description of your design and what you're trying to accomplish.
Jerry
 
landon manning
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This piece of the code is used to call 100+ different methods (and this number will grow, consider it potentially infinite). Each of these methods do their own specific task and some are only superficially related. Currently, we store a Integer/String (method name) combo in a hash table. When the doMethod( Integer i ) is called, it takes the Integer, looks up the method name in the hash table and then uses reflection to call that function.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you really need to pass the method name as a String, the best way to get rid of reflection is probably to use a HashMap that maps the String method name to an (anonymous) inner class that will call the desired method. All these inner classes will implement the same interface (call it MethodInvoker).
This solution is much more efficient than reflection and more elegant than a switch, but bulky: you will have to initialise your HashMap with lots of little anonymous classes. Building a little application that automatically generates this initialisation code may be worth your while.
If you don't need to pass the method name as a String then you might want to look into the GoF Command pattern.
Hope this helps
- Peter
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter,
I would like to pretend that I have memorized all of the Design Patterns, but sadly this is not true. Also, my copy of the book is at work.
So please remind me - GoF???
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gang Of Four
Design Patterns
by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Grady Booch (Designer)

Read some Good free Design Pattern books here!
Roseanne
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just out of curiousity, why is reflection not OO? I have
definitely heard reflection has lousy performance but it
seems like better OO since it is a way around that dreaded
switch.

Originally posted by Jerry Pulley:
Landon,
I agree that reflection is an inelegant, and not very O-O, solution to most problems. You could use a large switch statement to select the method to call, but that's hardly more elegant. Without seeing any of your design, I can only make a vague suggestion. You might use polymorphism by creating several subclasses of a single base. They'd all have the same method but different implementations - you could select which one to instantiate based on the argument, and the code that calls the method could do so through a reference to their base class.
If that doesn't help, post back some description of your design and what you're trying to accomplish.
Jerry


 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another vote for a Command pattern approach.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gang of Four - I knew that. Just a temporary memory lapse.
Thank you.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic