Use of Java getMethod and invoke

1 、 forName 方法

   forName is a static method, its role: to obtain the Class object corresponding to the class name by calling, and load the Class object in at the same time.

  If you save the class name in a string (such as xml), you can dynamically call the load when the program is running.

Note: Only available when the called parameter is a class name or method.

2. newInstance() method

   Role: to instantiate the object. The return type is Object. The difference with new is that new can take parameters, but newInstance() cannot. It initializes a non-parameter class at the same time. Usually used in conjunction with forName().

String str = "java.util.Date";
Class cl1= Class.forName(str);//加载java.util.Date类
Object obj = cl1.newInstance();//实例化cl1

3. The getMethod() method The
  getMethod method is similar to the getField method. The getField method returns a Field object based on the string representing the domain name. The getMethod method locates and returns the Method object that needs to be searched according to the method name and related parameters.

  The difference between getMethod and getDeclareMethods is that the latter returns an array of Method objects, and you need to find the required Method objects in the results yourself.

  原型: Method getMethod(String name,Class...parameterTypes)

   Parameter explanation: name: the name of the method

                       parameterTypes: the list of parameter types of the method (the order of the parameters should be arranged according to the parameter list when the method is declared)

   Returns: the method object conforming to the method name and parameters

   Throw an error: NoSuchMethodException   

                      Reason: The Method object to be queried was not found or the Method name is "<init>" or "<clinit>"

                       NullPointerException

                       Reason: The name of the Method object to be queried is null

                        SecurityException

                        Reason: the called class or its parent class does not have the calling permission

  Example:

Method m1 = Employee.class.getMethod("getName");
Method m2 = Employee.class.getMethod("raiseSalary",double.class);

  The above example obtains the method pointers m1 and m2 of the getName method and raiseSalary method of the Employee class.

4. Invoke method

  Role: Call the method wrapped in the current Method object.

   原型:Object invoke(Object obj,Object...args)

   Parameter explanation: obj: object after instantiation

                       args: parameters used for method invocation

   Return: According to the return value of the method called obj and args

   Throws an error: IllegalAccessException

                      Reason: Method object forces Java language execution control or does not have the right to access the obj object

                        IllegalArgumentException

                       Reason: The method is an instantiation method, and the specified object to be called is not the instantiated class or interface

  Example:

      Class l = Class.forName("test1.A"); 
      Object obj1 = l.newInstance(); 
      Object[] obj2 = new Object[1];
      obj2[0] = new String("hello world"); 
      Method m = l.getMethod("a1",new Class[] { String.class });
      Object obj3 = m.invoke(obj1, obj2);  


 

Guess you like

Origin blog.csdn.net/keepfriend/article/details/115371360