简体   繁体   English

Java动态(动态)调用方法

[英]Invoking method in a fly(dynamically) in Java

I am a newbie in Java. 我是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: 我想创建一个方法,该方法将第一个参数作为Function,将第二个参数作为Objects List,如下所示:

  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. 如果我将任何函数和参数列表传递给'dynamicMethodExecution',则它应通过传递参数来执行该函数。

This method 'dynamicMethodExecution' should be as generic as possible, ie it should take any kind of function and execute it on the fly. 此方法“ dynamicMethodExecution”应尽可能通用,即应采用任何功能并即时执行。

Any idea, how can I do this? 任何想法,我该怎么做?

If you're sticking with the built-in Function<T, F> class, then the apply method on this class does exactly that. 如果您坚持使用内置的Function<T, F>类,那么该类上的apply方法就是这样做的。 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. 但是,正如我在评论中所说, Function类仅表示一元函数,因此不能直接表示带有零参数的函数或带有多个参数的函数。

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. 我正在使用线程异步运行方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM