In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the method of Java rewriting the AST plug-in". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is Java's way to rewrite AST plug-ins"?
1. Introduction
With the release of Java 6, the java compiler already has an open source version. The open source compiler is part of the OpenJDK project, and http://www.openjdk.org/groups/compiler/ can be downloaded from the website of the Java compiler team. In the case of the examples in this document, however, any version of Java 6 is available because these examples do not recompile the compiler, they just extend the functionality of the compiler.
This article introduces the inherent implementation of the Java compiler. First we show the compilation steps included in the java compiler, and then we are writing two examples. Both examples use the plug-in mechanism in the compiler, that is, the mechanism described by JSR269. However, these two examples are beyond the scope of JSR269. By docking the JSR object with the compiler, we have implemented the rewriting of AST. In our example, instead of using the assertion statement, we use the if-throw statement.
2. Kernel of Java compiler
This section outlines the compilation steps and corresponding comments of the java compiler in OpenJDK. This section contains a brief introduction.
The compilation process is determined by the Java Compiler class defined in com.sun.tools.javac.main. When the compiler compiles with the default compilation parameters, it performs the following steps:
A) Parse: read in a pile of * .java source code and map the read Token to the AST node.
B) Enter: put the definition of the class in the symbol Symbol Table.
C) Process annotations: optional. Process the tags (annotation) found in the compilation unit (compilation units).
D) Attribute: add attributes to AST. This step includes name parsing (name resolution), type checking (type checking), and constant folding (constant fold).
E) Flow: perform a flow analysis (Flow analysis) operation for the previously obtained AST. This step includes a check for assignment and a check for reachability.
F) Desugar: rewrite AST and convert some complex syntax into general syntax.
G) Generate: generate source files or class files.
Wps_clip_image-12054_thumb
2.1 Parse
If you want a Parse file, the compiler uses the classes in com.sun.tools.javac.parser.*. As a * * step, the lexical analyzer (lexical analyzer) maps the input character stream (character sequence) to a symbol stream (token sequence). Parser then maps the generated symbol stream into an abstract grammar tree (AST)
2.2 Enter
In this step, the compiler finds all the definitions (definitions) found in the current scope (enclosing scope) and registers them as symbols (symbols). The Enter step is divided into the following two phases:
In the * * phase, the compiler registers all class symbols and associates the write symbols with the corresponding scope. The implementation is to use a Visitor (visitor) class to traverse the AST from top to bottom, accessing all classes, including inner classes within the class. Enter adds a MemberEnter object to the symbol of each class, which is called by the second stage
In the second phase, these classes are completed by the MemberEnter object (completed, that is, the Enter of the member variables of the completion class). First, MemberEnter determines the parameters, parent class, and interface of a class. These symbols are then added to the scope of the class. Unlike the previous step, this step is performed lazily. Members of a class are added to the definition of the class only when they are accessed. The implementation here is by installing a completion object (member object) into the symbol of the class. These objects can call member-enter when needed
*, enter puts all the top-level classes (top-level classes) into one todo-queue
2.3 Process Annotations
If there is a tag handler and the tag is specified in the compilation parameters, the process will process the tag in a compilation unit. JSR269 defines an interface that can be used to write this Annotation processing plug-in. However, the functionality of this interface is very limited and the language cannot be extended with Collective Behavior. The main limitation is that JSR269 does not provide reflective calls to submethods.
2.4 Attribute
Add attributes to all AST generated by the Enter phase. It should be noted that Attribte may require additional files to be parsed (Parse) and added to the symbol table through SourceCompleter.
Most environment-related analyses take place at this stage. These analyses include name resolution, type checking, and constant folding. These are all subtasks. Some subtasks call some of the following classes, but others may also be called.
L Check: this is the class used for type checking. When there is a completion error (completion error) or type error, it reports an error.
L Resovle: this is the class for name resolution. If the parsing fails, an error will be reported.
L ConstFold: this is the parameter collapse class. Constant folding is used to simplify constant expressions at compile time.
L Infer: the class referenced by the class parameter.
2.5 Flow
This phase performs a data flow check on the class after the attribute has been added. Survivability Analysis (liveness analysis) checks whether each statement can be executed. Exception analysis (Excepetion analysis) checks that every exception thrown by the bean is declared and whether these exceptions are caught. Determine row assignment (definite assignment) analysis ensures that each variable has been assigned when it is used. Deterministic definite unassignment analysis ensures that final variables will not be assigned multiple times.
2.6 Desugar
Remove redundant syntax, such as inner classes, class constants, assertion assertion statements, foreach loops, etc.
2.7 Generate
This is the final stage. Many source or class files are generated at this stage. Whether to generate a source file or a class file depends on the compilation option.
3. What is JSR 269?
Annotation (markup) is introduced in java 5 and is used to add meta information (meta-information) to the source code. Java 6 further enhances the tag processing function, namely JSR269. JSR269, the plug-in tag processing API, adds a plug-in mechanism to the java compiler. With JSR269, you have the ability to write a specific tag processor for the java compiler.
JSR269 has two basic sets of API, one for modeling the java language and one for writing tag handlers. These two groups of API exist in javax.lang.model.* and javax.annotation.processing respectively. The functionality of JSR269 is invoked through the following java compilation options.
-proc: {none,only} whether to perform Annotation processing or compilation
-processor specifies the name of the tag processor. This option bypasses the default tag processor lookup process
-processorpath specifies the location of the tag processor
Tag processing is turned on by default when it is in javac. If you just want to work with tags and don't want to compile and generate class files, you can use the-proc:only option.
4. How to print "Hello World!" with Javac
In this example, we have a simple tag handler for printing "Hello World!" at compile time. We use the compiler's internal message mechanism to print "hello world".
First, we define the following HelloWorld tags.
Public @ interface HelloWorld {}
Add a Dummy class that uses the above markup
@ HelloWorld public class Dummy {}
Tag processing can take a turn. Each round of processors only processes specific tags, and the generated source or class files are handed over to the next round for processing. If the processor is required to process only a specific round, it will also handle subsequent rounds, including the * round, even if the round has no tags to handle. The processor may also process the files generated by this tool.
The latter method processes the tag types generated in the previous round and returns whether the tags will be declared. If the return is True, then subsequent processors will not process them. If the return is false, then subsequent processors will continue to process them. A processor may always return the same logical value, or change the result according to the option. To write a tag handler, we use a subclass to inherit AbstractProcessor and mark the subclass with SupportedAnnotationTyps and SupportedSourceVersion. This subclass must override these two methods:
L public synchronized void init (ProcessingEnvironment processingEnv)
L public boolean process (Set
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.