0

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.

XPModder
  • 303
  • 4
  • 16
  • The main hurdle as it seems to me is that there's no way to tell if your input is a double, float, or int. You can try using `Integer.parseInt`, `Float.parseFloat` etc. but most numbers will work for both. – Yserbius Aug 09 '21 at 21:11
  • It's probably possible, but implementing and testing this super generic special function would probably take 10 times longer than just making those combinations. Alternatively try to redesign your program that those combinations is not necessary to begin with :-) – Tomas F Aug 09 '21 at 21:12
  • Simply duplicate the method and let it's copies accept other data-types, then cast as required (which is still a whole lot better & more readable than accepting `Object` and then testing with `instanceof`). – Martin Zeitler Aug 09 '21 at 21:18
  • Does this answer your question? [How can I make a Java function that accepts any type of variable?](https://stackoverflow.com/questions/50339778/how-can-i-make-a-java-function-that-accepts-any-type-of-variable) – Martin Zeitler Aug 09 '21 at 21:19
  • 3
    Why do you want to do this? It seems like an an [XY problem](https://xyproblem.info/) to me. – kaya3 Aug 09 '21 at 21:25
  • 1
    See [Run piece of code contained in a String](https://stackoverflow.com/questions/4389232/run-piece-of-code-contained-in-a-string) –  Aug 09 '21 at 22:54

0 Answers0