Get methods in Class:
public Method[] getMethods();//Get all public methods including itself and inheritance (implementation) - Method does not support generics <>, that is, no <> is followed
public Method[] getDeclaredMethods();//Get all methods (private, public, protected, have nothing to do with access rights). It does not include inherited methods that can directly obtain private attributes after jdk1.8. There is no need to set permissions, but only getDeclaredMethod method still needs to set permissions for Method methods.
public Method[] getMethod(String methodName, Class<T>...parameterTypes);// means to obtain a specified public method, including inherited parameters: methodName: means the name of the obtained method
parameterTypes: Class type representing the parameter of the retrieved method
public Method[] getDeclaredMethod(String methodName, Class<T>...parameterTypes);// means to obtain a specified method in this class (private, protected, public, irrelevant to access permissions), and does not include inherited methods
Class clazz = new Person().getClass(); try { //Calling the specified method/*@SuppressWarnings("unchecked") Method me = clazz.getDeclaredMethod("getName", String.class); me.invoke(clazz.newInstance(),"zhangsan"); */ //Get all methods Method[] methods = clazz.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()); } } catch (Exception e) { e.printStackTrace(); }For method calls with multiple parameters: the parameterTypes followed by getDeclaredMethod can be understood as a formal parameter of Class type. By calling invoke, the assignment of multiple parameters is best wrapped in new Object[]{}.
@SuppressWarnings("unchecked") Method method = clazz.getDeclaredMethod("getName",new Class[]{String.class,int.class}); method.invoke(new Person(), new Object[]{"zhangsan",10});Calling static methods
class User{public static void staticMethod(){System.out.println("static mthod invoke.");}}Eg:Class<User> clz=User.class;Method staticMethod=clz.getMethod("staticMthod"); There are two ways to call static methods:
1. Because the static method belongs to all instance objects, you can create an arbitrary object of the class and call it through the object.
staticMethod.invoke(clz.newInstance());//staticMethod has no parameters, so the parameter list type is not filled in.
2. If the underlying method is static, you can ignore the specified obj parameter and set the obj parameter to null
staticMethod.invoke(null);
More related content:
One: Reflection concept
You can obtain member variables and methods of a certain class through the Class class and call it.
2: Obtain methods, variables, and construction methods through reflection
@Test // Get class definition method public void testMethod() throws Exception { @SuppressWarnings("rawtypes") Class clazz = Class.forName("java.lang.String"); Method[] m = clazz.getDeclaredMethods(); for (int i = 0; i < m.length; i++) { System.out.println(m[i].getName() + " " + m[i].getDeclaringClass()); } } @Test // Get class definition variable public void testField() throws Exception { @SuppressWarnings("rawtypes") Class clazz = Class.forName("java.lang.String"); Field[] fields = clazz.getFields(); for (Field f : fields) { System.out.println(f.getName()); } } @Test // Get the constructor of class definition through reflection public void testConstructor() throws Exception { @SuppressWarnings("rawtypes") Class clazz = Class.forName("java.lang.String"); @SuppressWarnings("rawtypes") Constructor[] cons = clazz.getConstructors(); for (@SuppressWarnings("rawtypes") Constructor c : cons) { System.out.println(c + " " + c.getDeclaringClass()); } }Three: Call the method defined by the class through reflection
@Test // Call the method defined by the class through reflection public void testInvokeMethod() throws Exception { Class clazz = Class.forName("java.lang.String"); // Define the parameter type Class[] params = new Class[1]; params[0] = String.class; Method m = clazz.getDeclaredMethod("indexOf", params); // Set the parameter Object[] p = new Object[1]; p[0] = "e"; Integer s = (Integer) m.invoke("helloworld!", p); System.out.println(s); }