5

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

aioobe
  • 413,195
  • 112
  • 811
  • 826
dbtek
  • 928
  • 2
  • 12
  • 14

7 Answers7

26

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
  • 413,195
  • 112
  • 811
  • 826
3

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
  • 102,507
  • 33
  • 189
  • 228
3

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
  • 11,015
  • 1
  • 30
  • 44
2

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
  • 26,349
  • 9
  • 53
  • 79
  • "in runtime" suggest that the name of the method is not determined in compile time. – aioobe May 21 '10 at 14:34
  • 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 May 21 '10 at 14:37
  • Probably not. But I thought I'd add the exact answer to the question he asked, in case it was what he wanted. – DJClayworth May 21 '10 at 14:45
  • should we not use a = a.string(); – Thalaivar May 21 '10 at 14:48
2

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.

Edward Dale
  • 29,597
  • 13
  • 90
  • 129
2

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
  • 4,182
  • 1
  • 26
  • 44
0

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

dpc.pw
  • 3,462
  • 1
  • 19
  • 24