0

I am a newbie in Java. I want to create a method, that takes the first parameter as Function and the second parameter as a List of Objects as follows:

  public void dynamicMethodExecution(Somefunction someFunction, List<T> params) {
    //Pass the params to the someFunction and execute the someFunction.
  }

If I pass any function and any list of parameters to 'dynamicMethodExecution', then it should execute the function by passing the parameters.

This method 'dynamicMethodExecution' should be as generic as possible, i.e. it should take any kind of function and execute it on the fly.

Any idea, how can I do this?

Juvenik
  • 732
  • 4
  • 22
  • What do you mean by *"any kind of function"*? A [`java.util.function.Function`](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html)? A class method? An instance method (if so, for which instance)? – Robby Cornelissen Dec 27 '17 at 05:05
  • `Function ... Represents a function that accepts one argument and produces a result. ` The class you're using right now is only going to accept one argument. Not very generic, which seems to be your goal. – Silvio Mayolo Dec 27 '17 at 05:06
  • If you're talking about `java.util.function.Function` objects, your question has been answered by @SilvioMayolo. If you're talking about methods, I'm going to close your question as a duplicate of https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string – Robby Cornelissen Dec 27 '17 at 05:16
  • If you can provide your need in javascript or you familiar language it is easy to understand your need. Because you told you are new to java – janith1024 Dec 27 '17 at 05:20
  • This might be useful for you https://jmnarloch.wordpress.com/2015/10/17/java-8-functional-interfaces-and-varags-functions/ – Nikolai Shevchenko Dec 27 '17 at 05:37

2 Answers2

0

If you're sticking with the built-in Function<T, F> class, then the apply method on this class does exactly that. However, as I said in my comment, the Function class only represents 1-ary functions, so functions which take zero arguments or those which take multiple arguments could not be directly represented.

Silvio Mayolo
  • 24,199
  • 3
  • 34
  • 65
0

I finally did this. Please share ur view on it.

  public static void asyncMethodInvoke(Class<?> clazz, String methodName, Object[] args) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Class<?> params[] = new Class[args.length];
          for (int i = 0; i < params.length; i++) {
            if (args[i] instanceof Byte) {
              params[i] = Byte.TYPE;
            }
            else if(args[i] instanceof Short) {
              params[i] = Short.TYPE;
            }
            else if(args[i] instanceof Character) {
              params[i] = Character.TYPE;
            }
            else if(args[i] instanceof Integer) {
              params[i] = Integer.TYPE;
            }
            else if(args[i] instanceof Float) {
              params[i] = Float.TYPE;
            }
            else if(args[i] instanceof Double) {
              params[i] = Double.TYPE;
            }
            else if(args[i] instanceof Long) {
              params[i] = Long.TYPE;
            }
            else if(args[i] instanceof Boolean) {
              params[i] = Boolean.TYPE;
            }
            else {
              params[i] = args[i].getClass();
            }
          }

          Object _instance = clazz.newInstance();
          Method method = clazz.getDeclaredMethod(methodName, params);
          method.invoke(_instance, args);
        }
        catch (Exception e) {
          System.out.println(e.getCause().getMessage());
        }
      }
    }).start();
  }

I am using Thread to run method asynchronously.

Juvenik
  • 732
  • 4
  • 22