Java's reflection mechanism is especially suitable for large-scale projects, especially multi-functional projects. Because it greatly reduces the time of static loading classes at compile time and reduces the initial memory consumption.
Why is there a reflex mechanism?
1. When it comes to some applications, we often need to upgrade dynamically to increase the modification function. In static compilation system, all upgrade operations need to change the source code, which means that the whole source code must be compiled once for every upgrade. Small program compilation is fine, but if it is a large program, compilation can take several hours or even days to complete, so obviously the pure static programming method is not suitable. Java is also a static language, in order to remedy this shortcoming, there is a reflection mechanism.
2.Java is more often applied to server-side programs, and the program on the server has a very important feature that it can not be turned off; even if it is turned off, it should be notified to the user in advance. Then, if we want to upgrade the system at this time, we should not only ensure the normal operation of other functions in the system, but also ensure the upgrade measures. At this time, we can use reflection to achieve.
3. Make the program clearer, separate the main program from the functional program, and facilitate later management.
Application of Reflection Mechanism
1. Online Upgrade of Antivirus Software Virus Library
2. Construction of JDBC Data Bridge (Class.forName("Database Driven Package Class Name")
3. Increase in application functionality
4. Analyzing the Essence of Generics
5. Analyzing programs and finding bug s
There are three ways to implement reflection mechanism
1. Get the class type and load the class Class class = Class.forName("Something Name")
2. Given the class object, get its Class class = new Demo().getClass() from the object.
3. Get the class type Class = Demo. Class directly from the class
Class of reflection
1.Class class, which is used to deal with class-level reflection;
2.Field class, which handles reflection at member variable level;
3.Method class, which is used to deal with method-level reflection;
4.Constructor class, which is used to deal with constructor reflection.
A small example of reflection (for example, a management system)
import java.lang.reflect.Method;
import java.util.Scanner;
class Main {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
System.out.println("||Add|| ||Remove|| ||Sort|| ||Change|| ||Seek||");
System.out.print("Input your choose:");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
scanner.close();
try{
Class class1 = Class.forName(string); //Get classes and load classes at the same time
Method method = class1.getDeclaredMethod("command", String.class);
method.invoke(class1.newInstance(), "something");
}
catch(Exception e){
e.printStackTrace();
}
}
}
class Add implements Common{
public void command(String string){
System.out.println("Add Completed!");
}
}
class Remove implements Common{
public void command(String string){
System.out.println("Remove completed!");
}
}
class Sort implements Common{
public void command(String string){
System.out.println("Sort completed!");
}
}
class Change implements Common{
public void command(String string){
System.out.println("Change completed!");
}
}
class Seek implements Common{
public void command(String string){
System.out.println("Seek completed!");
}
}
interface Common {
public void command(String string);
}
Generics through Reflection
The essential function of generics in Java is to prevent input errors, type reference errors and parameter errors during compilation. After compilation, there is no concept of generics in Java. Here, it can be verified by reflection.
import java.lang.reflect.Method;
import java.util.ArrayList;
public class Test {
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args){
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("string");
System.out.println(arrayList.size());
Class class1 = arrayList.getClass();
try {
Method method = class1.getMethod("add", Object.class);
method.invoke(arrayList, 100);
System.out.println(arrayList.size());
}
catch (Exception e){
e.printStackTrace();
}
ArrayList arrayList2 = new ArrayList();
Class class2 = arrayList2.getClass();
System.out.println(class1 == class2);
}
}
The results show 1, 2, and true, which indicates that through reflection, the value of int type is successfully added to the collection whose original generic type is String, while the final true also indicates that the two collection class types are the same after compilation, and there is no generic type.