Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Reflection .getMethod int vs Integer [duplicate]

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...

Update This is NOT the same question as in here, since there one invokes directly, here there is a method in between that boxes the int into an Integer. As I mentioned before, that was my thought, but wanted other's opinions and knowledge on the matter.

like image 403
regina_fallangi Avatar asked Nov 19 '15 20:11

regina_fallangi


People also ask

Is Java reflection slow or expensive?

Adding setAccessible(true) call makes these reflection calls faster, but even then it takes 5.5 nanoseconds per call. Reflection is 104% slower than direct access (so about twice as slow). It also takes longer to warm up.

How do I stop reflection in Java?

The enabler of the technique used to avoid the very slow reflection is the invokedynamic bytecode instruction. Briefly, invokedynamic (or “indy”) was the greatest thing introduced in Java 7 in order to pave the way for implementing dynamic languages on top of the JVM through dynamic method invocation.

What is Java Lang reflect method?

The reflected method may be a class method or an instance method (including an abstract method). A Method permits widening conversions to occur when matching the actual parameters to invoke with the underlying method's formal parameters, but it throws an IllegalArgumentException if a narrowing conversion would occur.

What is method invoke in Java?

Java Method invoke() Method The invoke () method of Method class Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters automatically to match primitive formal parameters.


1 Answers

Yes, you can use int.class or Integer.TYPE to identify an integer primitive.

like image 197
Diogo de Góes Zanetti Avatar answered Sep 25 '22 21:09

Diogo de Góes Zanetti