Java reflection comprehension and Java reflection Comprehension
Effect of reflection: 1. runtime check class structure 2. change the field value of the class during runtime. 3. method preparation for calling classes: Class: A list of classes for each object saved by the Virtual Machine: static Class forName (String className) obtains the Class Object of the Class corresponding to the string (interface or full Class Name of the Class. Object newInstance () returns an Object parsing of the Class corresponding to the Class: 1. runtime check class structure java. lang. reflection has three classes: 1. the getName () Field of the Field Class returns the corresponding Field name getType () and returns the Class type of the Field 2. the getName () Method of the corresponding Class returns the name Class of the Method <?> [] GetParameterTypes () returns an integer from the parameter type array int getModifiers () in the Order declared by the method. It uses different bit switches to indicate static and public modifiers. 3. the Constructor corresponds to the Constructor getName () and returns the Class name corresponding to the Constructor. <?> [] GetParameterTypes () returns an integer from the parameter type array int getModifiers () in the Order declared by the method. It uses different bit switches to indicate static and public modifiers. In Method and Constructor, you can use the isPrivate and isStatic values of the Modifier class to determine the return value of getModifiers () and whether it contains the corresponding Modifier. The getDeclaredConstructors, getDeclaredMethods, and getDeclaredFields of Class objects are used to obtain the constructors, methods, and fields of the objects and return them as arrays. The basic reflection usage is as follows: Check the structure of a class:
1 import java. lang. reflect. constructor; 2 import java. lang. reflect. field; 3 import java. lang. reflect. method; 4 import java. lang. reflect. modifier; 5 6 // basic reflection Test 7 public class ReflectionTest {8 public static void main (String [] args) {9 String name = "java. util. date "; 10 try {11 Class cl = Class. forName (name); 12 Class supercl = cl. getSuperclass (); 13 String modifiers = Modifier. toString (cl. getModifier S (); 14 if (modifiers. length ()> 0) System. out. print (modifiers + ""); 15 System. out. print ("class" + name); 16 if (supercl! = Null & supercl! = Object. class) System. out. print ("extends" 17 + supercl. getName (); 18 19 System. out. print ("\ n {\ n"); 20 printConstructors (cl); 21 System. out. println (); 22 printMethods (cl); 23 System. out. println (); 24 printFields (cl); 25 System. out. println ("}"); 26} catch (ClassNotFoundException e) {27 e. printStackTrace (); 28} 29 System. exit (0); 30} 31 32 33 public static void printConstructors (Class cl) {34 Constructor [] constructors = cl. getDeclaredConstructors (); 35 36 for (Constructor c: constructors) {37 String name = c. getName (); 38 System. out. print (""); 39 String modifiers = Modifier. toString (c. getModifiers (); 40 if (modifiers. length ()> 0) System. out. print (modifiers + ""); 41 System. out. print (name + "("); 42 43 // print parameter types44 Class [] paramTypes = c. getParameterTypes (); 45 for (int j = 0; j <paramTypes. length; j ++) {46 if (j> 0) System. out. print (","); 47 System. out. print (paramTypes [j]. getName (); 48} 49 System. out. println (");"); 50} 51} 52 53 54 public static void printMethods (Class cl) {55 Method [] methods = cl. getDeclaredMethods (); 56 57 for (Method m: methods) {58 59 Class retType = m. getReturnType (); 60 String name = m. getName (); 61 62 System. out. print (""); 63 // print modifiers, return type and method name64 String modifiers = Modifier. toString (m. getModifiers (); 65 if (modifiers. length ()> 0) System. out. print (modifiers + ""); 66 System. out. print (retType. getName () + "" + name + "("); 67 68 // print parameter types69 Class [] paramTypes = m. getParameterTypes (); 70 for (int j = 0; j <paramTypes. length; j ++) {71 if (j> 0) System. out. print (","); 72 System. out. print (paramTypes [j]. getName (); 73} 74 System. out. println (");"); 75} 76} 77 78 79 public static void printFields (Class cl) {80 Field [] fields = cl. getDeclaredFields (); 81 82 for (Field f: fields) {83 Class type = f. getType (); 84 String name = f. getName (); 85 System. out. print (""); 86 String modifiers = Modifier. toString (f. getModifiers (); 87 if (modifiers. length ()> 0) System. out. print (modifiers + ""); 88 System. out. println (type. getName () + "" + name + ";"); 89} 90} 91}
2. change the Field value of the class: the Field Object has the following methods: public Object get (Object obj) gets the value of the Field on the target Object public void set (Object obj, Object value) set the values of fields on the target object to get and set methods of specific types. 3. method of calling class (similar to Method pointer): Method Object Method: public Object invoke (Object obj, Object... args) obj is the target object of the call, and ars is the sample code for the method parameters as follows:
1 import java. lang. reflect. field; 2 import java. lang. reflect. invocationTargetException; 3 import java. lang. reflect. method; 4 5/** 6 * Created by karlx on 2015/5/29. 7 */8 public class ReflectionTest2 {9 public static void main (String [] args) {10 Person person = new Person (); 11 person. name = "karl"; 12 // get, set the field value of the running object 13 Class clazz = person. getClass (); 14 try {15 Field field = clazz. getField ("name"); 16 field. setAccessible (true); // avoids java access check 17 System. out. println (field. get (person); 18 19 field. set (person, "xiaoming"); 20 System. out. println (field. get (person); 21 22} catch (NoSuchFieldException | IllegalAccessException e) {23 e. printStackTrace (); 24} 25 // Method of calling the object during running 26 try {27 method = clazz. getMethod ("getName"); 28 method. setAccessible (true); // avoid java access check 29 System. out. println (method. invoke (person); 30} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {31 e. printStackTrace (); 32} 33} 34 35 static class Person {36 public String name; 37 38 public String getName () {39 return name + "hello "; 40} 41 42 public void setName (String name) {43 this. name = name; 44} 45} 46}