• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can I call a method whose name is in a String?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've created a servlet for serving up Ajax request. This servlet will be a toolbox for users to easily extract XML for use in Ajax calls.

It will have a quite few methods for returning XML e.g. getContacts, getLatestNews etc. However, to call the correct method I have to do this:

if (userReq.equalsIgnoreCase("getLatestNews"));
getLatestNews(request, response);
if (userReq.equalsIgnoreCase("getContacts"));
getContacts(request, response);

I want a general purpose servlet rather than loads of them for each type of request.

I want to do something like:

callmethod(String);

Is there a way of doing this? I am using Java 1.5.

Thanks
Mark
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there definitely is, by using what's called reflection. Many of the tools you'll need are in the package java.lang.reflect. The basic idea: call java.lang.Class.getMethod() (or getMethods(), or getDeclaredMethods(), as needed) to get the java.lang.reflect.Method object for the method you want to call; then call its invoke() method with the appropriate arguments.
 
Mark Jones
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eh, that's great! I will look into those.

Thanks for the reply.

Mark
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your security auditors might go through the roof if they find out about this. You're exposing method names to the public and possibly permitting hackers to call any method on your object. It's awfully tempting to trust your users but maybe not the right thing to do.

Here's another way to do this. Look up the String you get in a map and get a little Java class that knows how to do that one thing. So "getContacts" might map to "com.mycompany.myproject.commands.GetContacts" and your code would be like:

All the commands implement the Command interface which has an execute() method. Now your big class is smaller - all those methods go into their own objects. Your if-else chain is gone, and you're not exposing any method names. If a hacker tries an invalid userReq the cmdClass comes up null.

I used Command for a name here but Strategy would be better. Read up on Strategy Pattern and see what you think.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic