0

Here's my code.

String name = "expevaluator." + super.left.getClass().getSimpleName() + super.right.getClass().getSimpleName() + "Addition";
        try {
            Object instance;
            instance = Class.forName(name).newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(Addition.class.getName()).log(Level.SEVERE, null, ex);
        }

I'm trying to call the evaluate function of the variable "name" class.

Here's the evaluate function.

public Number evaluate(int left, int right) {
    return left + right;
}

That's the evaluate method from IntegerIntegerAddition Class

and I have also the IntegerDoubleAddition Class evaluate method

public Number evaluate(int left, double right) {
    return left + right;
}

So the variable name could have "IntegerIntegerAddition" or "IntegerDoubleAddition" and I want with both of them call to the Evaluate method with the 2 parameters.

2
  • Okay, so you need to get the relevant method by reflection and invoke it - what have you tried for that part? Commented Apr 2, 2014 at 15:27
  • 1
    I'm not sure exactly what you're trying to achieve. I feel like theres a bit of missing context here with what you're trying to do. Commented Apr 2, 2014 at 15:28

2 Answers 2

2

You'll want to create a Method object using the name and parameter types that your real method will require. Then you want to use a reference to the object that has that method, and invoke your Method reference on it.

//These are all the types that my example references will use.

Object obj;  //The instance that my method is going to be invoked on.
Object[] params; //The parameters for my method
String nameOfMethod; //The name of my method.
Class<?>[] paramTypes; //The types, in order, that my method accepts as parameters

Method m = obj.getClass().getMethod(nameOfMethod, paramTypes); //Create my Method object

m.invoke(obj, params); //Invoke that method!

getMethod() accepts a vararg for the array paramTypes. So you can just list your types directly in there one by one as seperate parameters. Same thing with Method.invoke() for it's params parameter.

m.invoke(obj, param1, param2, param3) //etc
Sign up to request clarification or add additional context in comments.

Comments

0

You could do something like this:

Method evaluate = instance.getClass().getDeclaredMethod( "evaluate", int.class, int.class );
evaluate.invoke( instance, 3, 1 );

This should find the exact method by parameter type ( alternatively use "int.class, long.class" for your second method ) & invoke it, here using example arguments 3 & 1. Remember to catch the possible exceptions.

2 Comments

but I dont know the parameters primitive type it could be int int, int double, double int or double double
If you have overloaded methods that accept different parameters, logic will need to be put somewhere to determine which method is going to be chosen. This might mean combining your methods to accept more generic parameter types and do this type check internally, or checking the types of your values before instantiating the Method object, and having a case to create it using the correct types for the current situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.