JAVA reflection mechanism
The JAVA reflection mechanism is in the running state. For any class, you can know all the properties and methods of this class; for any object, you can call any method of it; this dynamically obtained information and dynamically called object. The function of the method is called the reflection mechanism of the Java language.
The Java reflection mechanism mainly provides the following functions: determine the class to which any object belongs at runtime; construct the objects of any class at runtime; determine the member variables and methods of any class at runtime; call any one at runtime A method of an object; generates a dynamic proxy.
1. Get the properties of an object
The code copy is as follows:
public Object getProperty(Object owner, String fieldName) throws Exception {
Class ownerClass = owner.getClass();
Field field = ownerClass.getField(fieldName);
Object property = field.get(owner);
return property;
}
Class ownerClass = owner.getClass(): Get the Class of this object.
Field field = ownerClass.getField(fieldName): Get the attribute declared by the class through Class.
Object property = field.get(owner): Get an instance of this property through the object. If this property is non-public, an IllegalAccessException will be reported here.
2. Get the static properties of a certain class
The code copy is as follows:
public Object getStaticProperty(String className, String fieldName)
throws Exception {
Class ownerClass = Class.forName(className);
Field field = ownerClass.getField(fieldName);
Object property = field.get(ownerClass);
return property;
}
Class ownerClass = Class.forName(className): First get the Class of this class.
Field field = ownerClass.getField(fieldName): Like above, get the attributes declared by Class through Class.
Object property = field.get(ownerClass): This is a bit different from the above, because the property is static, so it is taken directly from the Class of the class.
3. Methods to execute an object
The code copy is as follows:
public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception {
Class ownerClass = owner.getClass();
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(owner, args);
}
Class owner_class = owner.getClass(): First of all, you must get the Class of this object.
Lines 5 to 9: Configure the Class array of parameters as a condition for finding the Method.
Method method = ownerClass.getMethod(methodName, argsClass): Get the Method to be executed by the methodName and the argsClass (the parameter type set in the method) array.
method.invoke(owner, args): The parameters that execute the Method.invoke method are the object owner that executes this method, and the parameter array args. It can be understood as follows: the method method with parameter args in the owner object. The return value is Object, which is also the return value of the method.
4. Execute a static method of a certain class
The code copy is as follows:
public Object invokeStaticMethod(String className, String methodName,
Object[] args) throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Method method = ownerClass.getMethod(methodName,argsClass);
return method.invoke(null, args);
}
The basic principle is the same as Example 3, the difference is the last line. One parameter of invoke is null, because this is a static method and does not require the help of the instance to run.
5. Create a new instance
The code copy is as follows:
public Object newInstance(String className, Object[] args) throws Exception {
Class newoneClass = Class.forName(className);
Class[] argsClass = new Class[args.length];
for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
}
Constructor cons = newoneClass.getConstructor(argsClass);
return cons.newInstance(args);
}
The method mentioned here is to execute a constructor with parameters to create a new instance. If no parameters are needed, you can directly use newoneClass.newInstance() to implement it.
Class newoneClass = Class.forName(className): The first step is to get the Class of the instance to be constructed.
Lines 5 to 9: Get the Class array of parameters.
Constructor cons = newoneClass.getConstructor(argsClass): Get constructor.
cons.newInstance(args): Create a new instance.
6. Determine whether it is an instance of a certain class
The code copy is as follows:
public boolean isInstance(Object obj, Class cls) {
return cls.isInstance(obj);
}
7. Get an element in the array
The code copy is as follows:
public Object getByArray(Object array, int index) {
return Array.get(array,index);
}