A Java reflection mechanism example 2

Reprinted: https://blog.csdn.net/loyoveui/article/details/1662154

package test;

import java.lang.reflect.Method;

public class InvokeTest {
  /**
   *
   * main method
   * @param args
   * void
   */
  public static void main(String[] args) {
    try {
      InvokeTest invokeTest = new InvokeTest();
      //1. The first step to get the mapping of the method
      //String[] realArgs = {"",""};//Define a String array corresponding to the first parameter of the execute method (note that this array is only for the mapping of the method)
      //Integer in = new Integer(0);//Define an Integer object corresponding to the second parameter of the execute method      
      //Class[] c_args = new Class[2];
      //c_args[0] = realArgs.getClass();//Get the classes of the above two parameters respectively
      //c_args[1] = in.getClass();
      //Method method = invokeTest.getClass().getMethod("execute", c_args);//The return value is the mapping of the test method (type Method)
      /**
       * Note that the above sentences (lines 11-16) can be written as the following sentence
       * The purpose is to obtain the map of the execute method, the first parameter is the method, the second parameter is the list of parameters required by the execute method, and the type is class
       * So when the execute method has no parameters, the second parameter of getMethod can be null
       */
      Method method = invokeTest.getClass().getMethod("execute",
          new Class[] { String[].class, Integer.class });
      
      //2. The second step calls the method
      //String[] a1={"zhong","cheng"};//The array here is used for the actual call parameters
      //Integer a2=new Integer(5);//Same as above
      //Object[] allArgs = new Object[2];
      // allArgs [0] = a1;
      // allArgs [1] = a2;
      //Object[] result = (Object[])method.invoke(invokeTest, allArgs);//Call the execute method and get the return value
      /**
       * Note that the above sentences (lines 21-26) can be written as the following sentence
       * The purpose is to call the execute method of the instance invokeTest, the parameter (including a String array and an Integer object) type is Object
       * The first parameter of the invoke() method represents the instance of the class to which the called method belongs, so if execute is a static method,
       * The first parameter of invoke can be empty
       */
      Object[] result = (Object[])method.invoke(invokeTest, new Object[] {
          new String[] { "zhong", "cheng" }, new Integer(5) });
      // print out the return value
      for(int i=0;i<result.length;i++){
        System.out.print(result[i]);
      }
    } catch (Exception e) {
      e.printStackTrace ();
    }

  }

  /**
   *
   * The execute method for testing, here just prints the values ​​of String[] and Integer
   * and returns an informative string array
   * @param String[] str
   * @param Integer intN
   * String[]
   */
  public String[] execute(String[] str, Integer intN) {
    for (int j = 0; j < str.length; j++) {
      System.out.println(str[j]);
    }
    System.out.println(intN.intValue());
    return new String[]{"display ","have ","bean ","finished "," !"};
  }
}
xxx

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325506238&siteId=291194637