To execute Java code in the JVM must be compiled into a class file, where the JDK compiles Java code into a class file, which is often referred to as the Java source Code compilation mechanism.
1, the JVM defines the format of the class file, but does not define how to compile Java source code as a class file, each vendor in the implementation of the JDK will usually be compliant with the Java language Specification of the source code compiled into a class file compiler, such as the JDK is Javac
The steps to build a class file Javac compile are as follows:
1. Analysis and input to symbol table (parse and enter)
The parse process is lexical and syntactic parsing, and lexical analysis (Com.sun.tools.javac.parser.Scanner) is done by turning the code string into a token sequence (for example, Token.eq (name:=)); Com.sun.tools.javac.parser.Parser) to complete is to generate an abstract syntax tree from the token sequence based on the syntax.
The Enter (Com.sun,tools,javac.comp.enter) procedure is to enter symbols into the symbol table, usually including determining the superclass and interface of the class, adding the default constructor as needed, and the symbol table of the symbol input class that appears in the class is medium.
2. Annotation processing (Annotation processing)
This step is primarily used to handle user-defined annotation, and the potential benefit is to generate additional code or perform special checks based on annotation, saving some common code, such as when using Lombok, which can greatly reduce the amount of code, Compilation is the introduction of Lombok to the entity after compiling, and then through the Javap view class file, found automatically into get set ToString, and other methods, this feature is based on JSR269, support in JDK1.6, after annotation processing , enter the parse and enter step again
3. Semantic analysis and generation of class files (analyse and Generate)
Analyse steps based on the abstract syntax tree for a series of grammatical analysis, including the syntax tree names, expressions and other elements associated with variables, methods, types and so on, check whether the variables are declared before use, deduce the type parameters of the generic method, check the type of matching, constant folding; Check that all statements are reachable, Check that all checked exception are caught or thrown, check the deterministic assignment of the variable (if the method with the return value must have a return value), check the variable's deterministic non-repeating assignment (for example, a variable declared as final), and remove the syntax sugar (if (false)) form of useless code; Turn generics Java into plain Java, and change the syntax tree containing the syntax sugar to a syntax tree containing simple language constructs, such as for Each loop, auto boxing, unpacking, etc.
After the semantic parsing is completed, the steps to generate a class file (Com.sun.tools.javac.jvm.Gen) are started, first the instance member initializer phone to the constructor, and the static member initializer phone as <cinit> (); Then the abstract syntax tree generated bytecode, the method for the post-order traversal of the syntax tree, and the final small amount of code conversion (such as String addition to the StringBuilder operation), and finally generated from the symbol table class file.
Generating a class file in addition to Javac, you can compile Java source code into a class file via ECJ (Eclipse compiler for Java) or a compiler such as Jikes.
The class file not only stores bytecode, but also stores a number of auxiliary JVMs to execute class extensions, and a class file includes
-Structure Information
Includes the class file format version number and the number and size information for each part
-Meta data
Simple can be considered as the method of information on the use of Java source code in the statement, the expression of the corresponding information, mainly byte code, exception processing table, evaluation stack and local variable size, the type of evaluation stack records, debugging with symbolic information.
Implementation mechanism of Java code of "Java"