This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Refactoring Reflection (any suggestions?) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Refactoring Reflection (any suggestions?)" Watch "Refactoring Reflection (any suggestions?)" New topic
Author

Refactoring Reflection (any suggestions?)

landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
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?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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

Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6919
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?


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
landon manning
Ranch Hand

Joined: Nov 20, 2000
Posts: 47
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.
Jerry Pulley
Ranch Hand

Joined: Sep 19, 2000
Posts: 221
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

Joined: Nov 20, 2000
Posts: 47
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.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Nov 14, 2000
Posts: 1953
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
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
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

Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Another vote for a Command pattern approach.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Gang of Four - I knew that. Just a temporary memory lapse.
Thank you.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Refactoring Reflection (any suggestions?)
 
Similar Threads
java Main()
Garbage Collection
Java reflection - method invokation?
How to get method parameter names
constructor with private access modifier