jeudi 19 novembre 2015

Java Reflection .getMethod int vs Integer

I have a class MyClass in package mypackage which looks like this:

package mypackage;
public class MyClass{

    public static myMethod (Integer i) {
        return i + " is an Integer";
    }

    public static myMethod (int i){
        return i + " is an int"
    } 
}

There is also a class Invoker, which looks like this:

public class Invoker{

    public String call(String class_name, String method_name, Serializable[] parameters){
        int p_amount = parameters.length;
        Class<?> param_types = new Class[p_amount];

        // Get types of parameters
        for (int i = 0; i < p_amount; i++){
            param_types[i] = parameters[i].getClass();
        }

        // Get method with that signature
        Method m = Class.forName(class_name).getMethod(method_name, param_types); 

        // Invoke method with given parameters
        m.invoke(null, parameters); 
    } 
}

Here is what happens when invoking the method:

int number = 3;
new Invoker().call("mypackage.MyClass", "myMethod", number)//->i is an Integer

Is there a way to specify that I may be calling the method with int as an argument?

My guess is that when int gets passed as a Serializable object, it automatically gets parsed to its cousin, the real object, Integer, but I am not a Java expert...





Aucun commentaire:

Enregistrer un commentaire