Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function by its name, given from string java

I would like to be able to call a function based on its name provided by a string. Something like

public void callByName(String funcName){
   this.(funcName)();
}

I have searched a bit into lambda funcions but they are not supported. I was thinking about going for Reflection, but I am a bit new to programming, so I am not so familiar with the subject.

This whole question was brought up on my java OOP class, when I started GUI (Swing, swt) programming, and events. I found that using object.addActionCommand() is very ugly, because I would later need to make a Switch and catch the exact command I wanted.

I would rather do something like object.attachFunction(btn1_click), so that it would call the btn1_click function when the event click was raised.

like image 972
Joao Avatar asked May 31 '10 11:05

Joao


People also ask

How do you call a function in Java?

To call a method in Java, simply write the method's name followed by two parentheses () and a semicolon(;).

How do you call a method from an object in Java?

The dot ( . ) is used to access the object's attributes and methods. To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main.java).

When invoking a method What do you need to specify before the method name and after the method name?

This particular method returns an integer value which is assigned to an integer variable named number. You invoke (call) a method by writing down the calling object followed by a dot, then the name of the method, and finally a set of parentheses that may (or may not) have information for the method.


3 Answers

Java has methods, not functions. The difference is that methods have classes; you need to know the class to call the method. If it's an instance method, you need an instance to call it on, but OTOH it does mean that you can look the method up easily:

public void callByName(Object obj, String funcName) throws Exception {
    // Ignoring any possible result
    obj.getClass().getDeclaredMethod(funcName).invoke(obj);
}

Note that there are a lot of potential exceptions out of this and things get more complex if you want to pass arguments in.

If you are talking about a class method, what you do is slightly different:

public void callClassByName(Class cls, String funcName) throws Exception {
    // Ignoring any possible result
    cls.getDeclaredMethod(funcName).invoke(null);
}

You might also want to explore using a java.lang.reflect.Proxy.

like image 61
Donal Fellows Avatar answered Nov 15 '22 03:11

Donal Fellows


The following code will call a public method of the given name, with no argument:

public void callByName(String funcName) {
    try {
        Method method = getClass().getDeclaredMethod(funcName);
        method.invoke(this, new Object[] {});
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}
like image 25
Maurice Perry Avatar answered Nov 15 '22 03:11

Maurice Perry


You can use Class.getDeclaredMethod() to get the method with a specific name and parameters, then invoke the returned Method object:

Method method = MyClass.class.getDeclaredMethod("methodName", Event.class, String.class);
Object retVal = method.invoke(someEvent, someString);
like image 31
Péter Török Avatar answered Nov 15 '22 03:11

Péter Török