java reflection gets and invokes methods
- 2020-12-09 00:50:28
- OfStack
Get method in Class class:
public Method[] getMethods(); // Get all public methods, including themselves and their inheritance (implementation) -- Method does not support generics
<
>
, that is, the back is not connected
<
>
public Method[] getDeclaredMethods(); // Get all of its own methods (private, public, protected, regardless of access), not inherited
Methods that can get private properties directly after jdk1.8 do not require permissions but are restricted to getDeclaredMethod methods and still need to be set for Method methods
Permissions.
public Method[] getMethod(String methodName, Class
<
T
>
. parameterTypes); // means to get a specified public method, including inherited methods
Parameter: methodName: represents the name of the obtained method
parameterTypes: Represents the Class type for the parameters of the fetched method
public Method[] getDeclaredMethod(String methodName, Class
<
T
>
. parameterTypes); // means to get one of the specified methods in this class (private, protected, public, regardless of access), excluding inherited methods
Class clazz = new Person().getClass();
try {
// Invokes the specified method
/*@SuppressWarnings("unchecked")
Method me = clazz.getDeclaredMethod("getName", String.class);
me.invoke(clazz.newInstance(),"zhangsan");*/
// Get all the methods
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
Method calls for multiple parameters: getDeclaredMethod followed by parameterTypes can be understood as a parameter of type Class, and the assignment of multiple parameters by calling invoke 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 a static method:
1. Since static methods are common to all instance objects, 1 arbitrary object of the class can be created and invoked through the object
staticMethod.invoke(clz.newInstance()); //staticMethod has no arguments, so the parameter list type is not filled
2. If the underlying method is static, the specified obj parameter can be ignored and the obj parameter set to null
staticMethod.invoke(null);
1: Reflection concept
You can get the member variables and methods of a class from the Class class and call them.
2: Get methods, variables, constructors by reflection
@Test
// Gets the methods defined by the class through reflection
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
// Gets the variables defined by the class through reflection
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
// Gets the constructor of the 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());
}
}
3: Invoke a class-defined method through reflection
@Test
// Invokes a class-defined method through reflection
public void testInvokeMethod() throws Exception {
Class clazz = Class.forName("java.lang.String");
// Define parameter types
Class[] params = new Class[1];
params[0] = String.class;
Method m = clazz.getDeclaredMethod("indexOf", params);
// Set the parameters
Object[] p = new Object[1];
p[0] = "e";
Integer s = (Integer) m.invoke("helloworld!", p);
System.out.println(s);
}