当前位置:网站首页>4. Jctree related knowledge learning
4. Jctree related knowledge learning
2022-07-02 15:26:00 【At the beginning of dawn】
1. hum about
1.1. AST
1.1.1 Java Three stages of compilation
- Previous blogs (2. Customize Java Compile time annotation processor ), Annotate the processor at compile time process() When the method is used , Such a diagram is given to illustrate that annotation processing is multi round

- This picture clearly shows Java The three stages of source code compilation
- Parse and Enter: Java The source file is parsed into an abstract syntax tree (Abstract syntax tree,AST)
- Annotation Processing: Scan the annotation , Call the corresponding compile time annotation processor to process annotations . This process may modify existing source files or generate new source files , These source files will enter again Parse and Enter Stage for processing
- Analyse and Generate: analysis AST And into class file
- The above description may not be very accurate , It is certain that :(1)Parse Stage will parse the source code into AST,(2) Annotation processing stage may produce new Source code , Annotation processing is multi round
1.1.2 AST
- AST This term , Yes, a lot IT It's no stranger to small partners in the field
- such as , Engaged in distributed SQL Development colleagues ( Developers close to the bottom of the component ), Often say SQL Implementation process of :
- SQL The sentence is analyzed by morphology 、 After parsing , Is parsed into AST
- AST Transform into logic Edit query plan , The logical query plan is transformed into Distributed query plan
- The distributed query plan is transformed into Physical query plan , These query plans will be distributed to the execution node (worker) To perform
- worker After summarizing the execution results on , Returned to client
- The principle of compilation was learned when I was an undergraduate , For lexical analysis 、 I don't know the details of grammar analysis
- The most impressive : The source code is represented in a tree structure , The elements in the source code will be mapped to AST A node or a subtree in
- for example ,
5 + (12 * 1)Finally, the corresponding AST as follows . Interested in , You can continue to read in depth :AST series ( One ): Why is abstract syntax tree abstract
- Blog Android AOP And AST: Abstract syntax tree , Gives a code AST Example .

- Many nodes above , stay JDK There are corresponding classes in : for example ,ClassDecl Corresponding JCTree.JCClassDecl,Literal Corresponding JCTree.JCLiteral
1.2 JSR 269
- JSR 269 yes JDK 6 A set of specifications for annotation enhancement in , Its full name is Pluggable Annotation Processing API, Plug in annotation processor interface
- JSR 269 There are two basic API, A set of processors for writing annotations , One group is used for java Language modeling
javax.annotation.processing.*: Customize the compile time annotation processor APIjavax.lang.model.*: Put the member method 、 Variable 、 Constructors 、 Interfaces, etc. Java The element is mapped to Element and Type(TypeMirror)
- JSR 269 Annotation processor implemented by specification , Annotations can be processed during compilation
- here , Annotation processor is equivalent to a plug-in of compiler , So called Plug in annotation processor .
- Reference link :
2. JCTree
- JDK Of tools.jar in , There is one
com.sun.tools.javac.treepackage , There are a lot of people in it Java Compile time AST Related classes , Such as JCTree、TreeMaker、TreeTranslator etc. - If you also use Intellij IDEA, But found JCTree Unable to view source code , Please refer to my previous blog : To configure Intellij IDEA To see tools.jar Source code
- If you use Intellij IDEA, You can use the Navigate → \rightarrow →Type Hierarchy View the sub class or sub interface of the interface
- Specific reference document :idea View all subclasses of a class and subclasses of a subclass and display them in hierarchical relationships
- I use the flagship version with an account IDEA, Community Edition IDEA Can you show , To be verified
2.1 Tree
- Introducing JCTree Before , You should first introduce
com.sun.source.treeAll kinds of... In the bag Tree Interface
Tree And JCTree The relationship between
- Tree Interface is AST Public interface of all nodes in ,Tree And its sub interfaces correspond to AST Specific nodes in
- Tree The interface and its sub interfaces are composed of JDK compiler (javac) Realization , It should not be implemented directly or indirectly by other applications
- So-called javac Realization , This is the focus of this article
com.sun.tools.javac.treeIn bag JCTree And its subclasses
Tree Interface
Tree The interface is very simple
One represents all tree Enumeration class of type
Tree.Kind, Containing one associatedInterface Field , Used to describe the constant Association Tree A subinterface ( I think I say Tree The node type is more accurate )A return Tree Corresponding
Tree.KindOfgetKind()MethodA use visitor Pattern implementation
accept(TreeVisitor<R,D> visitor, D data)Method , Through this method, we can realize AST Node operation ;- The generic parameter R Represents the return value of the operation ,D Represents additional data needed to perform the operation
- In the later study , We will experience accept The role of methods
public interface Tree { public enum Kind { // The specific content is omitted } Kind getKind(); <R,D> R accept(TreeVisitor<R,D> visitor, D data); }
2.2 JCTree
JCTree abstract class
JCTree yes AST The root class of the node , Internal nesting defines the corresponding specific AST Subclasses of nodes , And each subclass is highly standardized
In order to
com.sun.source.treeAll kinds of... In the bag Tree Interfaces are different ,JCTree And its subclasses are represented byJC(javac) startJCTree There are only two fields ,pos and type, Represent the location and type of nodes in the source file respectively
JCTree Added an abstract accept Method , Its subclasses will implement the abstract method , To put the given visitor Act on AST( node )
public abstract void accept(Visitor v);
JCTree Subclasses of
- JCTree The subclasses of are as follows , among JCExpression and JCStatement Is the parent of many other subclasses

- Introduce some JCTree An important subclass of
- JCStatement: Statement node
- JCBlock: Statement block node
- JCReturn:return Statement node
- JCVariableDecl: Variable definition node
- JCClassDecl: Class defines the node
- JCMethodDecl: Method definition node
- JCExpression: Expression nodes
- JCAssign: Assignment statement node
- JCLiteral: Constant value node for a given literal
- JCIdent: Identifier node ( I don't quite understand , But I found a lot code example Are used to identify a class )
treeMaker.Ident(names.fromString("this")) treeMaker.Ident(names.fromString("String")
- JCModifiers: Modifier node , Such as PUBLIC、NATIVE、ABSTRACT etc. , For details, see
com.sun.tools.javac.code.Flagsclass
- JCStatement: Statement node
- About JCTree And its subclasses , You can refer to the blog : Reprint : Abstract syntax tree AST A comprehensive analysis of ( Two ) ( It is more recommended to read the source code , And combine code example To study )
Unable to get new Create a syntax tree node
- The author is learning JCTree And its subclasses , Once wanted to pass new One JCVariableDecl object , Look at the meaning of each field , To help learn JCVariableDecl
- JCVariableDecl The first parameter of is JCModifiers example , To identify the access rights of variables or other modifiers
- therefore , Create a JCModifiers example , result IDEA Tips JCModifiers The constructor for is protected jurisdiction

- After reading the source code carefully , Find out JCTree The constructor of each subclass uses protected modification , If not
com.sun.tools.javac.treeClass in or not its subclass , You can't create AST node - JCTree As an abstract class , You cannot create instances
- terms of settlement : adopt
TreeMakerRealization AST Node creation
2.3 JCTree.Visitor & TreeTranslator
JCTree There is an internal abstract class
Visitor,VisitorClass is defined with visit The first method of accessing tree nodes , Such as visitClassDef()、visitMethodDef()Reality class TreeTranslator A general tree translator pattern is defined
The translator can follow AST, From top to bottom 、 Traverse the tree nodes from left to right , Build translation nodes by overwriting existing nodes
Inherit TreeTranslator And rewrite Visitor The corresponding method in the class , You can perform specific operations on tree nodes , To delete 、 Modify or add tree nodes
for example , The following code shows how to pass visitor Schema modification method name
private class Inliner extends TreeTranslator { // Want to modify the method node , Then rewrite visitMethodDef Method @Override public void visitMethodDef(JCTree.JCMethodDecl jcMethodDecl) { super.visitMethodDef( jcMethodDecl ); // If the method is called doing getUserName Then change its name to testMethod if (jcMethodDecl.getName().toString().equals( "getUserName" )) { JCTree.JCMethodDecl methodDecl = make.MethodDef( jcMethodDecl.getModifiers(), names.fromString( "testMethod" ), jcMethodDecl.restype, jcMethodDecl.getTypeParameters(), jcMethodDecl.getParameters(), jcMethodDecl.getThrows(), jcMethodDecl.getBody(), jcMethodDecl.defaultValue ); this.result = methodDecl; // Update the original method node } } }
2.4 JCTree.Factory & TreeMaker
As mentioned above , because protected Access rights , Not directly new One AST node , But it can go through TreeMaker Create
TreeMaker yes
JCTree.FactoryImplementation class of interface ,Factory Interfaces are created AST Dedicated interface of node , and TreeMaker It's about creating AST Factory class of node ( Factory method Design patterns )TreeMaker Yes Factory Interface , The implementation of abstract methods is very simple :new A corresponding AST node , Update the pos, then return This node instance
With JCAssign For example , The factory method is defined as follows
public JCAssign Assign(JCExpression lhs, JCExpression rhs) { JCAssign tree = new JCAssign(lhs, rhs); tree.pos = pos; return tree; }TreeMaker The constructor of is also protected type , Therefore, you cannot directly create TreeMaker example
TreeMaker Class provides a
instance(Context context)Static methods , Can be used to create TreeMaker exampleamong ,Context It must be the context of an environment , Create directly context Will report a mistake
public static void main(String[] args) { Context context = new Context(); TreeMaker treeMaker = TreeMaker.instance(context); Names names = Names.instance(context); // Set the modifier of the variable 、 name 、 Type and initial value JCTree.JCVariableDecl variableDecl = treeMaker.VarDef(treeMaker.Modifiers(Flags.PUBLIC), names.fromString("name"), treeMaker.Ident(names.fromString("String")), treeMaker.Literal("lucy")); System.out.println(variableDecl.toString()); }adopt context establish TreeMaker Times wrong

Refer to the connection :
- treeMaker Introduction and practical examples : Java The art of killing dragons in —— How to modify the syntax tree
- The original English text :Dragon killing in Java: how to modify the syntax tree?
3. summary
- Java Source code compilation
- Parse the source code into AST, Then call the compile time annotation processor to process annotations
- The annotation processor can create a new source file or modify an existing source file ( adopt JCTree Realization AST Modification of )
- The source code processed by the annotation processor will be parsed again , So annotate the processor process Method will run multiple rounds , Until it's done
- Generate class Bytecode , complete Java Source code compilation
- Java Of AST
com.sun.source.treeIn bag Tree Interfaces and their sub interfacescom.sun.tools.javac.treeIn bag JCTree Abstract classes and their subclasses : Implement the corresponding Tree Interface , Corresponding Java Nodes in the syntax tree- JCTree Abstract inner classes in Visitor、Visitor Subclasses of TreeTranslator: use visitor Design pattern implementation is right Java Syntax tree node operation , The essence :JCTree Of accept() Method call visitor The specific visit Method , Realize to Java Syntax tree node operation
- JCTree Internal interface in Factory、Factory Implementation class of TreeMaker: Because a syntax tree node cannot be instantiated in the application , Can pass TreeMaker Create ( Factory approach design pattern )
边栏推荐
- JVM architecture, classloader, parental delegation mechanism
- The past and present lives of visual page building tools
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- Tidb data migration tool overview
- Mavn builds nexus private server
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
- Recommended configuration of tidb software and hardware environment
- 学习使用php实现公历农历转换的方法代码
- I made an istio workshop. This is the first introduction
猜你喜欢

工程师评测 | RK3568开发板上手测试

17_ Redis_ Redis publish subscription

JVM architecture, classloader, parental delegation mechanism

Practical debugging skills
![[noi Simulation Competition] scraping (dynamic planning)](/img/ee/27a07f80207a2925f5065e633eb39f.png)
[noi Simulation Competition] scraping (dynamic planning)

Build your own semantic segmentation platform deeplabv3+

Leetcode - Search 2D matrix

搭建自己的语义分割平台deeplabV3+

Case introduction and problem analysis of microservice

06_栈和队列转换
随机推荐
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
How to write sensor data into computer database
Application of CDN in game field
List set & UML diagram
LeetCode_ String_ Simple_ 412.Fizz Buzz
Evaluation of embedded rz/g2l processor core board and development board of Feiling
03.golang初步使用
How to conduct TPC-C test on tidb
学习使用php实现公历农历转换的方法代码
08_ 串
[c voice] explain the advanced pointer and points for attention (2)
Oracle primary key auto increment
HUSTPC2022
13_ Redis_ affair
TiDB数据迁移场景综述
08_ strand
Data analysis thinking analysis methods and business knowledge - business indicators
16_ Redis_ Redis persistence
10_ Redis_ geospatial_ command
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant