The files we usually write are Java files, and finally we need to compile the Java files into a class (binary file) to run on the JVM.
In the implementation of the JDK, each vendor usually conforms to the Java language specification to compile the compiler as a class file. The compiler is good or bad to a large extent determines the final program on the machine to perform efficiency. The Sun JDK (JAVAC) is typically used.
Javac the following main three steps for compiling Java source code into class files:
Parse and input to symbol table (parse and enter)
The parse process is lexical analysis and parsing. Lexical analysis is mainly to convert a string into a token sequence; parsing is based on the syntax of creating an abstract syntax tree of tokens.
Enter is to enter the symbol into the symbol table. Typically, you include determining the superclass and interface of a class, adding a default constructor as needed , and medium the symbol table of the symbol input class that appears in the class.
annotation processing (annotation processing)
Primarily dealing with user-defined annotations, we typically generate a code or perform special checks based on annotations. The associated code is generated at compile time. Then do the parse and enter operation.
Semantic analysis and generation of class files (analyse and generate)
Analyse is a series of semantic analysis based on the abstract syntax tree, including the names in the syntax tree, expressions and other elements associated with variables, methods, types, etc., check whether the variables are declared before, to deduce the type parameters of the generic method, to check the type matching, to make constant folding, to check that all statements are reachable ; Check that all exception are captured or thrown, check the deterministic assignment of variables, convert generics to Java, and reduce grammatical structure;
After the semantic analysis is completed, the class file is started: the instance member initializer is first collected into the constructor, the static member initializer is collected as <clinit> (), and then the abstract syntax tree is generated, followed by a sequential traversal of the syntax tree, and the last few code conversions (such as converting a string to a stringbuffer operation), and finally generating a class file from the symbol table.
The class file does not only contain bytecode, but it also stores a number of auxiliary JVMs to perform class-attached information.
Java's compilation mechanism