This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes substitute string in methods name? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "substitute string in methods name?" Watch "substitute string in methods name?" New topic
Author

substitute string in methods name?

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have something like:




you see, the getX is a Method-Name.

Now, I have something like this:




Unfortunately, this does not work. Is there another way to achieve this?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10744
    
    9

What are you trying to achieve exactly? Looks like you want to figure out at runtime which method should be invoked. Is that correct?

One way to do it is to get a list of all methods using reflection, iterate through them, get the names, compare the method name with your string and invoke it if required.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14952
    
  26

Reflection is the way to do this.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 43950
    
  33
nimo frey wrote: . . .
You would mean "a.get" + what; surely? Maneesh and Jesper have already answered.
 
Have you checked out Aspose?
 
subject: substitute string in methods name?
 
It's not a secret anymore!