当前位置:网站首页>[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
2022-07-06 00:34:00 【Programmer community】
List of articles
- One 、 Use annotations to AST Syntax tree conversion
- 1、 Define annotations and use GroovyASTTransformationClass Indicate the AST Adapter
- 2、AST Implementation of conversion interface
- 3、 Definition Groovy Class and use @Compile Annotations modify the methods that need to be intercepted
- 4、 Execution results
One 、 Use annotations to AST Syntax tree conversion
1、 Define annotations and use GroovyASTTransformationClass Indicate the AST Adapter
First , Definition Compile annotation , The annotation name is any string , @Target(ElementType.METHOD)
Indicates that the annotation acts on the method , @GroovyASTTransformationClass("MyASTTransformation")
Indicates that the node modified by this annotation corresponds to AST The implementation class of the conversion interface is MyASTTransformation
;
import org.codehaus.groovy.transform.GroovyASTTransformationClassimport java.lang.annotation.ElementTypeimport java.lang.annotation.Target/** * This annotation works on methods */@Target(ElementType.METHOD)@GroovyASTTransformationClass("MyASTTransformation")@interface Compile {
}
2、AST Implementation of conversion interface
then , Realization MyASTTransformation class , Inheritance of such kind ASTTransformation Interface ;
import org.codehaus.groovy.ast.ASTNodeimport org.codehaus.groovy.ast.builder.AstBuilderimport org.codehaus.groovy.ast.stmt.BlockStatementimport org.codehaus.groovy.control.SourceUnitimport org.codehaus.groovy.transform.ASTTransformationimport org.codehaus.groovy.transform.GroovyASTTransformation@GroovyASTTransformationclass MyASTTransformation implements ASTTransformation {
/** * Compile time processing * @param nodes AST Abstract syntax tree node , yes ASTNode An array type * @param source Source unit , You can get the source file through this object */ @Override void visit(ASTNode[] nodes, SourceUnit source) {
// obtain Groovy.groovy A collection of classes in a script , And traverse // stay ModuleNode The class nodes in are encapsulated in the following members // List<ClassNode> classes = new LinkedList<ClassNode>(); source.AST.classes.find {
// The search name is Student Class // it yes ClassNode node it.name == "Student" }?.methods?.find {
// lookup Student The name under the class is hello Methods // it yes MethodNode node it.name == "hello" }?.with {
// eureka Student Under the hello Method // stay MethodNode Call under the node // it Namely MethodNode node BlockStatement blockStatement = code // Empty BlockStatement Medium List<Statement> statements member // Method intercepts emptying , No longer execute the original method // Method injection does not empty , Will execute the original method content //blockStatement.statements.clear() // Create a method node def methods = new AstBuilder().buildFromSpec {
expression {
methodCall {
variable('this') constant('println') argumentList {
constant('hello buildFromSpec') } } } } // Add the method node to hello In the method //blockStatement.statements.addAll(methods) // Create a method node def methods2 = new AstBuilder().buildFromString('println "hello buildFromString"') // Add the method node to hello In the method //blockStatement.statements.addAll(methods2) // Create a method node , Note that what you get here is def methods3 = new AstBuilder().buildFromCode {
println "hello buildFromCode" } // Add the method node to hello In the method blockStatement.statements.addAll(methods3[0].statements) } }}
3、 Definition Groovy Class and use @Compile Annotations modify the methods that need to be intercepted
Last , Realization Groovy class , Use on the methods of this class @Compile annotation ;
class Student{
def name @Compile def hello(){
println "hello" }}def student = new Student()student.hello()
4、 Execution results
Carry out the above Groovy Script , The execution result is :
hellohello buildFromCode
边栏推荐
- LeetCode 1598. Folder operation log collector
- LeetCode 6005. The minimum operand to make an array an alternating array
- Pointer pointer array, array pointer
- notepad++正則錶達式替換字符串
- 2022-02-13 work record -- PHP parsing rich text
- OpenCV经典100题
- Yolov5, pychar, Anaconda environment installation
- 《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
- LeetCode 6004. Get operands of 0
- 免费的聊天机器人API
猜你喜欢
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
MySql——CRUD
Go learning --- structure to map[string]interface{}
Priority queue (heap)
数据分析思维分析方法和业务知识——分析方法(三)
Basic introduction and source code analysis of webrtc threads
Atcoder beginer contest 254 [VP record]
Atcoder beginer contest 258 [competition record]
Determinant learning notes (I)
Classic CTF topic about FTP protocol
随机推荐
【文件IO的简单实现】
Browser local storage
Room cannot create an SQLite connection to verify the queries
Hudi of data Lake (1): introduction to Hudi
FFmpeg抓取RTSP图像进行图像分析
数据分析思维分析方法和业务知识——分析方法(三)
XML配置文件
关于slmgr命令的那些事
【DesignMode】组合模式(composite mode)
云导DNS和知识科普以及课堂笔记
数据分析思维分析方法和业务知识——分析方法(二)
Determinant learning notes (I)
Cloud guide DNS, knowledge popularization and classroom notes
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Spark SQL空值Null,NaN判断和处理
Classic CTF topic about FTP protocol
STM32 configuration after chip replacement and possible errors
Extracting profile data from profile measurement
MySQL storage engine
Model analysis of establishment time and holding time