lundi 2 mars 2015

determine the resulting class of an expression

Is there an easy way to decide the resulting Java Class of a Java mathematical expression? For example, I can calculate the resulting class of something like String + double but is there something out there give the resulting class of any valid expression of mixed classes at runtime?


Background: I need this as I am using Java reflection to call methods with arguments at runtime. The arguments have been parsed from text into Expression objects where Expression is my class for handling expressions. An Expression can be made up of other Expressions. So for instance I may be parsing code like:



double a = 0;
double b = 0;
MyObject object = new MyObject();
object.getSomething(a, b, (c+d+e)/f);


I know the Classes of a and b because they are defined variables but the Class of third argument needs to be calculated before I can do something like:



Method method = MyObject.class.getMethod("getSomething", double.class, double.class, ????);





Aucun commentaire:

Enregistrer un commentaire