Hi there!
I'm having problems with trying to get a method with reflection that has primitive types as arguments in a dynamic way.
The problem is this:
my program tries to call methods dynamically with information he gathered from previous runs.
example method:
public class Calculator {
public float addition(float a, float b) {
return a + b;
}
}
The gathered information from 1 run with this code:
new Calculator().addition(3, 4)
gives me:
Class: calculator.Calculator
Method: addition
Returnvalue class: java.lang.Float
Returnvalue : 7.0
Parameters : java.lang.Float 3.0 java.lang.Float 4.0
So he gathers this information correctly, but only one problem:
He sees the class of the parameters as instances of java.lang.Float instead of the primitive type float.
So if I would try to do this:
Calculator.class.getDeclaredMethod("addition", new Class[]{java.lang.Float.class, java.lang.Float.class});
My program would do this dynamically with the information he gathered from above.
Then I would get this error:
java.lang.NoSuchMethodException: calculator.Calculator.addition(java.lang.Float, java.lang.Float)
Because the method is declared with primitive types as parameters and therefore he can't find it.
So my question is: how can I solve this problem?
One thing I tried was to in the code of the information gatherer I would see if it's a java.lang.Float
then I would cast the class to float.class.
Ofcourse with that solution my above code works perfectly.
But if the programmer would make the method like this:
public Float addition(Float a, Float b) {
return a + b;
}
with Float from java.lang.Float then my workaround would not work because he would give me this:
java.lang.NoSuchMethodException: calculator.Calculator.addition(float, float)
So to keep things short, I would like to know if there is a way to call methods dynamically with reflection
and it wouldn't matter if the parameters are primitive types or their wrapper for java.
I hope you guys can see what my problem is, I found it hard to describe it in words...
Greetings from Belgium!
Stijn
ps: sorry for my bad english, I normally speak dutch