• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to call multiple methods based on what the user asks for ?  RSS feed

 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say my class has 20 methods. I want the user of my program to be able to decide which ones
he/she wants to call. If he enters meth1, meth2, meth3, meth4 out of the 20, then my code calls only
4 methods in succession.

What would be the best way to do this ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
justin smythhe wrote:What would be the best way to do this ?

I hate to say, but probably reflection.

However, before you go charging off down that road, have a look at your design. Do you really need to do this?

Is there some other way you could interpret these "choices"?
Instead of calling separate methods, could they perhaps be interpreted as calling the same method on different classes? If so, you could use polymorphism instead.

My advice: Avoid reflection if you possibly can; it's almost always the wrong choice. And if it isn't, then Java is probably the wrong language.

Winston
 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
justin smythhe wrote:What would be the best way to do this ?

I hate to say, but probably reflection.

However, before you go charging off down that road, have a look at your design. Do you really need to do this?

Is there some other way you could interpret these "choices"?
Instead of calling separate methods, could they perhaps be interpreted as calling the same method on different classes? If so, you could use polymorphism instead.

My advice: Avoid reflection if you possibly can; it's almost always the wrong choice. And if it isn't, then Java is probably the wrong language.

Winston


Thanks a million Winston ! Why do you say that reflection is bad ? It seems convenient to me.

As an aside, how does reflection really work behind the scenes ? Which part of JRE/Java handles
the reflection ? Lets call it the rHandler. If this rHandler can show me fields, methods etc (here), can it
be made to show the entire code of the class ? In other words, can we reverse engineer rHandler
and then modify it to do that task ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
justin smythhe wrote:Thanks a million Winston ! Why do you say that reflection is bad ? It seems convenient to me.

And that's because it basically allows you to do anything to anything without having to worry about worry about the normal constraints of the language; and if that's what you want, why did you choose Java in the first place? Most scripting languages allow you to do that stuff much more easily.

For good or ill, Java is statically typed. For me, it's one of its great strengths, but it also means that it's not suitable for certain tasks; particularly ones that involve late binding - and reflection is about as late as it gets. There are a few situations where it's warranted; but outside really basic stuff like getClass(), I can still count the number of times I've used it in 11 years on the fingers of both hands.

As an aside, how does reflection really work behind the scenes ?

Ooof. The mechanics you'd probably have to read up on; but basically, Java has a bunch of meta-classes (classes that contain information about classes - eg, Class, Method, etc) and "reflection" involves calls to methods in those classes.

The trouble is that they aren't simple; and they're also generally SLOW (up to 50 times slower than regular method calls according to Josh Bloch), so as a result reflection code tends to be complex, difficult to follow, error-prone...and of course, SLOW. If it is absolutely what you need, then obviously speed shouldn't be a consideration; but more often than not there are better alternatives. Sometimes, they just require a bit more thought.

If this rHandler can show me fields, methods etc (here), can it be made to show the entire code of the class ? In other words, can we reverse engineer rHandler and then modify it to do that task ?

The basic answer to your question is: probably, yes. Reflection allows you to do pretty much anything you like. You can even use it to modify Strings. And that's the problem:
Presumably one of the reasons you chose Java was because of some of the stupid things it stops you from doing (after 10 years of C and C++, it's certainly why I started using it), so if you simply turn to reflection every time you have a tricky problem, you're basically abandoning the language.

My 2 cents, FWIW.

Winston
 
justin smythhe
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
My 2 cents, FWIW.

Winston


Thanks Winston ! I like the way you explained it. Btw, your 2 cents is like $2mil for me
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a design which asks the user which methods they want to call, then your design needs to be rewritten. If I'm a user of your application then I shouldn't have to deal with implementation details like that. The application is there to do things which I want done, not to run these strangely-named "methods", whatever they are.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
justin smythhe wrote:Thanks Winston ! I like the way you explained it. Btw, your 2 cents is like $2mil for me

I wish some of it was coming my way. However, you're most welcome.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!