Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling dynamicly created method names 1

Status
Not open for further replies.

Madere

Technical User
Apr 14, 2003
85
GB
I have a question about calling dynamicly created method names.
First of all: can this be done?

What I mean is the following:
We have a class Test with several (static) methods: ConvertA(), ConvertB(), ConvertC(), ConvertD(), ...

In our programm we get the letter of the method (eg A or B or C ...)
Then we want to concatenate "Test.Convert" with the letter (eg "A") and the pharentesis and the call the method Test.ConvertA().
So our String concatenation will be like: "Test.Convert" + "A" + "()" (Maybe with 1 or more parameters).

Then the concatenated string is contaning the calling object and method name (with optional paramaters).
Then how to call the right class/method with that string?
If necessary only the method name can be concatenated.

We do not want to use a switch or if statement and then call the method.

Does anyone has an idea if it is possible and how to do it?

Thanks,

Madere.
 
Yes, reflection would solve the problem, but why don't you like switches?

a) They are fast. (reflection is expensive)
b) They are more sure (early binding, compiler checks)
c) They fit well to a single-character:
Code:
case 'A': Workerclass.ConvertA (); break;
case 'B': Workerclass.ConvertB (); break;
// this will be difficult with runtime-generated names:
case 'C': Workerclass.ConvertB (foo, bar); break;
[code]
 
stefanwagner :

The post requires a means to call methods DYNAMICALLY - not hard-code switch/if statements !!!

Also, re the performance, we use reflection all the time, mainly in our JMX apps, and the performace is brilliant. Many J2EE applications and APIs rely on vendor-JMX - which uses reflection constantly.

Hard-coding in the switch statements means that if another app needs to call a method not specified in your staetment means a recompile and redeploy of the application - now thats expensive !
 
I have to say that I only use reflection as my last chance. If I can determine a method name at compile time, I will never use reflection.

But I must recognize that's also a powerful tool when you have to write generic applications with object of unknown types, invoke protected or private methods and other kinf of stuff.

Cheers.

Dian
 
Gents,

my questions is answered and the problem solved.
Thanks Diancecht!

Stefanwagner: when I write we do NOT want to use switches, please do not suggest it.
Suppose you have to create a switch of over 25 cases?
That is a lot of code in the main java file and not very maintainable. With reflection the code is smaller and you are more flexibel.

Madere.
 
sedj:
I ASKED why Madere doesn't like switch-statements.

Of course switches are very fast, especially if you have 25 or 2500 cases.
You needent code all those cases by hand.

Code which uses reflection is much harder to read, harder to obfucate, harder to debug etc.
There ARE payoffs using reflection, and if it is okay from the performance, and you feel good about it, it's absolut okay.
Of course it's no proof, that the performance of switch-statements are much better.

If you need a new Method, you have to recompile something too - the costs depend on a lot of things, only Mandere can tell.
And there are possibilities in design, to minimize the dependencies from the switch-statement (Interface, abstract class).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top