I am trying to create a way to give a full method call as a string to a function, which then should try to actually call this method.
This should work with any type and any number and generally arbitrary parameters.
The class where the methods are declared is going to be fixed (probably given as an argument to this classes constructor)
Basically it should work, as if the contents of the String (whatever they might be) would be a line inside the function.
Obviously this function can only try to do this and would need to catch and handle all exceptions (eg. when that method doesn't exist in the given class).
Like this:
public class Thisclass{
String example = "Testmethod(1, 4.23, 'test')";
Class otherclass;
public void Thisclass(Class implementationClass){
this.otherclass = implementationClass;
}
//Calling myFunction like this: myFunction(example);
//Should result in this being called: otherclass.Testmethod(1, 4.23, "test");
public void myFunction(String input){
try{
//Something here
}
catch(Exception ex){
//Handle exceptions
}
}
}
I know how I could do this when I knew the number, type and order of the parameters using reflection, but can I do the same with an unknown number of parameters of unknown type and in an unknown order?
I would be ok with limiting the maximum total number of parameters to something relatively small (eg. 4 or 5), but I would like to leave the type and order of the parameters relatively unlimited.
It would also be ok to limit the types to something like int, float, double, char and String
plus arrays of all these.
I know I could overload a method with all sorts of possible parameters, but that is very unpractical, as only allowing a maximum of 4 integers or integer arrays would already bring me up to 8 different versions and allowing a random mix of those two would already need 31 different implementations...
Is this even possible?
Edit:
The comments tell me, that some of you don't really understand what I want to do.
Basically I am parsing a HTML file to be rendered using opengl and I want to be able to make links or buttons in the HTML file execute some method, but I do not want to specify one method, but leave the methods to a different part of the project.
Think:<a href="MyMethod(1, 4.23, 'test')">Run Method</a>
where MyMethod can be an arbitrary Method. The only thing that is known about the method is that it is implemented in the class otherclass
. After parsing the HTML I can get the value of the href attribute as a string, so basically "MyMethod(1, 4.23, 'test')"
.
For those that suggested that it would be easiest to create / copy a method for every possible combination and order of the data types mentioned above: I did a quick calculation and only accepting 1-4 parameters of the 5 types mentioned above in an arbitrary combination and order, would require over a million different versions of the method. I really doubt that creating all those and somehow not ending with missing or doubled methods at the end is faster than anything else.
The best suggestion so far is the one from saka1029, to basically create an all new java file and compile it using the compiler api and then to run it. I will look into that, as it seems to be the best idea so far.