Method Reflect - When have class,method name and args;how to reflect a method

26 Views Asked by At

el: defined some method such as:

  class AClass {
    public void dynamicMethod(String a, String... b) {
      log.info("dynamic String arg:" + a);
    }

    public void dynamicMethod(String a, Integer... b) {
      log.info("dynamic Integer arg:" + a);
    }

    public void dynamicMethod(String a, Object... b) {
      log.info("dynamic Object arg:" + a);
    }
  }

JVM can execute correctly:

    @Test
    public void testDynamicArgs() throws Exception {
        this.dynamicMethod("String", "s1", "s2");
        this.dynamicMethod("Integer", 1, 2);
        this.dynamicMethod("Object", "1", null, 2);
    }

console:

dynamic String arg:String
dynamic Integer arg:Integer
dynamic Object arg:Object

so when have class this.getClass(),method name dynamicMethod,args "1", null, 2;how to determine the right Method.Because arguments maybe contain null variable number or even both.Determine the parameter type in turn is not an efficient way.Is there a tool method provided in the JVM or other open source projects.

  public Method getMethod(Class<?> clazz, String methodName, Object[] args) {
    // TODO how write there;
  }
0

There are 0 best solutions below