views:

211

answers:

7

How can we call a method which name is string at runtime. Can anyone show me how to do that in Java and C.

Thanks for replying.

+4  A: 

In java it can be done through the reflection api.

Have a look at Class.getMethod(String methodName, Class... parameterTypes).

A complete example (of a non-static method with an argument) would be:

import java.lang.reflect.*;
public class Test {

    public String methodName(int i) {
        return "Hello World: " + i;
    }

    public static void main(String... args) throws Exception {
        Test t = new Test();
        Method m = Test.class.getMethod("methodName", int.class);
        String returnVal = (String) m.invoke(t, 5);
        System.out.println(returnVal);
    }
}

Which outputs:

Hello World: 5

aioobe
+3  A: 

In Java:

If class A has a method "string()" then you call it by:

A a = new A();
a.string();

C does not have methods and you can't call them. You may be thinking of C++ which essentially has exactly the same syntax.

DJClayworth
"in runtime" suggest that the name of the method is not determined in compile time.
aioobe
I somehow don't think that's what the OP meant. I understand the question as dynamic function resolution - thus, reflection in Java, and not quite but close, function pointer lookup table in C.
Amadan
Probably not. But I thought I'd add the exact answer to the question he asked, in case it was what he wanted.
DJClayworth
should we not use a = a.string();
Vinothbabu
+1  A: 

In Java you would use reflection:

Class<?> classContainingTheMethod = ...; // populate this!
Method stringMethod = classContainingTheMethod.getMethod("string");
Object returnValue = stringMethod.invoke(null);

This is a very simple case that assumes your method is static and takes no parameters. For non-static methods, you'd pass in the instance to invoke the method on, and in any case you can pass in any required parameters to the invoke() method call.

Andrzej Doyle
+2  A: 

In Java, you'll have to use the Java Reflection API to get a reference to the Method object representing your method, which you can then execute.

scompt.com
+1  A: 

In C (or C++) real reflection is not possible as it is a compiled language.

The most used is to have an associative container (a map) that can link a function name (as a string) to a function pointer. You have to fill in the map in the program with the value you want. This can't be done automatically.

You could also simply have a function that takes a string as parameter and then chose the right function to call with hand-made ifs.

Nikko
+2  A: 

Here is a base C example, I hope it will help you.

typedef void (*fun)(void);

static void hello()
{
  puts("hello world");
}

static void string()
{
  puts("string");
}

static void unknown()
{
  puts("unknown command");
}

struct cmd 
{
  char* name;
  void (*fun) (struct cmd* c);
};

static struct cmd commands[] = {
  { "hello", hello },
  { "string", string },
  { 0, unknown }
};


static void execute(const char* cmdname)
{
  struct cmd *c = commands;

  while (c->name && strcmp (cmdname, c->name))
    c++;
  (*c->fun) (c);
}

int main()
{
  execute("hello");
  execute("string");
  execute("qwerty");
}
Aif
+1 for the only c approach
stacker
That's still a function and not a method, though.
Tyler McHenry
A: 

I'm quite sure you can put all your functions into the shared library and load them with dlopen + dlsym.

dpc.ucore.info