Java bytecode and reflection mechanism

    Byte Code is an important guarantee for the cross-platform features of the Java language and an important foundation for the reflection mechanism. Through the reflection mechanism, we can not only see the attributes and methods of one class, but also call the methods of another class in one class, but only if we have the bytecode file of the related class (that is, the .class file).

1 Bytecode and .class files

    After we write a file with a .java extension, if it can be run (for example, it contains the main function), then we can run the .java file by clicking the run button in MyEclipse.

    But at this time, MyEclipse hides a key step from us: it first compiles the .java file into a bytecode file with an extension of .class, and then, the Java Virtual Machine (JVM) is on the current operating system ( For example, run this .class file on window 10).

    In other words, the .java file must be compiled into a .class file before it can be run. What’s even more amazing is that the .class file compiled in the Windows 7 system can be run directly in the Linux system (of course, this system has to be There is a Java runtime environment). This is the cross-platform feature of Java, which is also called the ability to "compile and run everywhere".

    I’m a bit off topic. Back to the topic of reflection. As long as we can get the bytecode file of .class, then through the reflection mechanism we can not only see the attribute method and other information in the java file corresponding to this .class, but also call It corresponds to the method in the java file.

2 Class class (C is uppercase) is the grammatical basis of reflection implementation

    With some tools, we can open the .class file and see the properties and methods contained in it, but we can't directly program the .class file. We have to use the Class (C is uppercase).

    The full name of the Class class is java.lang.Class. When a class or interface (in short, the class file after the java file is compiled) is loaded into the Java Virtual Machine (JVM), a java associated with it will be generated .lang.Class object, in the reflection part of the code, we generally access and use the properties and methods of the target class through this Class.

    Through the reflection mechanism, the attributes of the specified class can be seen from the .class file, such as the attribute modifier, the attribute and the variable name of the type and attribute. Through the following ReflectionReadVar.java, we look at the specific method of demonstration.    

1	import java.lang.reflect.Field;
2	import java.lang.reflect.Modifier;
3	class MyValClass{
4		private int val1;
5		public String val2;
6		final protected String val3 = "Java";
7	}

    We defined a MyValCalss class in line 3, and defined three attribute variables in lines 4 to 6.    

8	public class ReflectionReadVar {
9		public static void main(String[] args) {
10			Class<MyValClass> clazz = MyValClass.class;
11			//获取这个类的所有属性
12	        Field[] fields = clazz.getDeclaredFields();
13		    for(Field field : fields) {
14		    	   //输出修饰符	    	   System.out.print(Modifier.toString(field.getModifiers()) + "\t");
15		    	   //输出属性的类型
16		       System.out.print(field.getGenericType().toString() + "\t");
17		    	   //输出属性的名字
18		    	   System.out.println(field.getName());
19		      }
20		}
21	}

     In the 10th line of the main function, through MyValClass.class, we get the variable clazz of type Class<MyValClass>. In this variable, some information of the class MyValClass is stored.

    In line 12, the clazz.getDeclaredFields() method is used to obtain the information of all the attributes in the MyValClass class, and store the information of these attributes in the fields variable of the Field array type.

    The for loop that passed the 13th line outputs these attribute information in turn. Specifically, the modifier of the attribute is output by the code on line 14, the type of the attribute is output by the code on line 16, and the variable name of the attribute is output by the code on line 18. The output of this code is as follows, from which we can see the information of each attribute.

  1. private    int val1
  2. public class java.lang.String   val2
  3. protected final   class java.lang.String   val3

Guess you like

Origin blog.csdn.net/sxeric/article/details/113058457