Java reflection
Reflection: in a word, every component in Java is parsed into a corresponding Java class. Every component in Java is parsed into a class.
1. Class class
The class represents the Java class, and its instance corresponds to the bytecode in the memory of the instance. When each class is loaded into the memory, it is parsed into bytecode.
The class instance indicates the bytecode.
Method to get the class instance object:
1) Direct class name call: person. Class;
2) object call: new person (). getclass ();
3) load from memory: class. forname ("Java. Lang. String ");
2. Three important classes of Java reflection:
1) constructor class: represents a constructor in the class.
Constructor <string> constructor = string. Class. getconstructor (string. Class); // only obtain the public method.
// Obtain the constructor. Input a parameter and the parameter is a constructor of the string type.
String string = (String)constructor.newInstance(new String("abc"));
// Reflect the instantiated object through the constructor. The input parameters must be the same as those obtained in the obtained constructor.
2) method: represents a method in the class. The following code uses the reflection member method:
System. out. println ("---------------- member Reflection Method -------------------------"); methodcharat = string. class. getmethod ("charat", Int. class); string str1 = "ABC"; // The following invoke must first specify the object to call the method specified above, followed by the input parameter. System. Out. println (methodcharat. Invoke (str1, 1); // methodcharat. Invoke (null, 1); // This indicates a static method call.
Note: The reflected main method needs to wrap the parameters in the main method. Jdk1.5 is compatible with previous JDK versions. Splits incoming data into a single object.
System. out. println ("---------------- reflection main method -------------------------"); Method mainmethod = Class. forname ("com. test. person "). getmethod ("Main", string []. class); // system. out. println ("main method reflection error parameter passing method. "); // Mainmethod. invoke (null, new string [] {"AB", "ABC"});/*** exception: wrong number of arguments ***** // *** JDK to be compatible with JDK, input an array of the object type (string directly inherits the object ), invoke automatically disconnects * each of the elements is used as a parameter. In this way, the number of parameters does not match. * It may be because jdk1.5 supports variable parameters, but before jdk1.5 does not support variable parameters. */System. Out. println ("the first parameter passing Method for main method reflection. "); // Here we will package the array into a package again, so that an array will be added when it is split. Mainmethod. invoke (null, (object) New String [] {"AB", "ABC"}); system. out. println ("the second parameter passing method reflected by the main method. "); // Here, it is forcibly converted into an object to cheat the compiler. // this way, the compiler will not perform the split operation. The input is still an array. Mainmethod. Invoke (null, new object [] {New String [] {"AB", "ABC "}});
3) represents a field in the class.
Field fieldX = pt1.getClass().getDeclaredField("x");
If it is a private variable, it cannot be output, so you can perform brute force reflection:
fieldX.setAccessible(true);
3. Effect of reflection:
1) Implement Dynamic proxy
2) implementation framework