Learning about getDeclaredMethod (). invoke() in reflection comes from a line of code in the project:
SubjectService.class.getDeclaredMethod(autoMatchConfig.getMethodName(), Integer.class).invoke(subjectService, GlobalConfig.OPEN_TO_IPLAN);
The methods of acquiring reflection are as follows:
The first is: Class c = SubjectService.class
Second species:
Class c = Class.forName(SubjectService) Once you get the Class, you can get the reference method.
c.getDeclaredMethod(String name, Class<?>... parameterTypes)
Get all the methods in this class (only in this class)
c.getDeclaredMethods(); Continue parsing in the code
SubjectService.class.getDeclaredMethod(autoMatchConfig.getMethodName(), Integer.class) The first parameter is the method name and the second parameter is the method parameter. After passing in the two parameters, the method with parameters can be obtained by reflection based on the method name and method parameters. Then
MethodName.invoke(subjectService, GlobalConfig.OPEN_TO_IPLAN) Among them, the invoke method imports the instance object of the method obtained in the previous step and the argument of the method imported. Thus, method names and parameter names are obtained by reflection, and invoke method injects method objects and parameters.
GetDeclared Method: Returns the Method method object;
invoke: Call methods by configuring parameters based on the incoming object instance
For example:
package com; public class Person { private int age; private String name; public Person( String name,int age) { this.age = age; this.name = name; } public Person() { } //Public-Owned Reference Method public void public_show(String str,int i) { System.out.println("public show "+str+"..."+i); } //Public-owned or non-public-owned methods public void public_prin() { System.out.println("public prin"); } //Private participation method private void private_show(String str,int i) { System.out.println("private show "+str+"..."+i); } //Private method private void private_prin() { System.out.println("private prin"); } }
public class Main { public static void main(String[] args) throws Exception { //Get the method in the bytecode file and then extract its public method String classname = "com.Person"; //Find the class file with the name, load it into memory and generate the class object Class cl = Class.forName(classname); //Get a Person object System.out.println("Get one Person object:"); Object obj=cl.newInstance(); System.out.println(); //1. Get the public void demo2.Person.public_prin() Method Person_public_prin=cl.getMethod("public_prin",null); System.out.println("Get execution public void demo2.Person.public_prin() :"); Person_public_prin.invoke(obj,null); System.out.println(); //2. Get public void demo2.Person.public_show(java.lang.String,int) Method Person_public_show=cl.getMethod("public_show",String.class,int.class); System.out.println("Get execution public void demo2.Person.public_show(java.lang.String,int) :"); Person_public_show.invoke(obj,"Amazing me",12); System.out.println(); //3. Get private void demo2. Person. private_print () Method Person_private_prin=cl.getDeclaredMethod("private_prin",null); Person_private_prin.setAccessible(true); System.out.println("Get execution private void demo2.Person.private_prin() :"); Person_private_prin.invoke(obj,null); System.out.println(); //4. Get the private parameter method private void demo2.Person.private_show(java.lang.String,int) Method Person_private_show=cl.getDeclaredMethod("private_show",String.class,int.class); Person_private_show.setAccessible(true); System.out.println("Get execution private void demo2.Person.private_show(java.lang.String,int) :"); Person_private_show.invoke(obj,"Mysterious Private Ownership",23); System.out.println(); }
Part of the code comes from: https://blog.csdn.net/qq_35146878/article/details/78504268