当前位置:网站首页>[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
2022-07-06 00:35:00 【Programmer community】
List of articles
- One 、 stay MyASTTransformation#visit Method injection
- 1、 Use new AstBuilder().buildFromSpec Conduct method injection
- 2、 Use new AstBuilder().buildFromString Conduct method injection
- 3、 Use new AstBuilder().buildFromCode Conduct method injection
- Two 、 Complete code examples and the compilation process of compile time processing
- 1、Groovy Script Groovy.groovy
- 2、ASTTransformation Interface implementation MyASTTransformation.groovy
- 3、 To configure ASTTransformation
- 3、 Use the command line for compile time processing
One 、 stay MyASTTransformation#visit Method injection
stay 【Groovy】 Compile time metaprogramming ( Compile time method interception | stay MyASTTransformation#visit Method interception in method ) Blog Method injection based on method interception ;
First of all, MethodNode To deal 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()
If you will blockStatement.statements
Cleaned up , No more execution Student#hello The original method content ;
Retain blockStatement.statements
The original set element , Continue adding other elements to it , Other contents can be executed on the basis of the original method ;
1、 Use new AstBuilder().buildFromSpec Conduct method injection
First create a method node ,
// Create a method node def methods = new AstBuilder().buildFromSpec {
expression {
methodCall {
variable('this') constant('println') argumentList {
constant('hello buildFromSpec') } } } }
Then the method node , Add to blockStatement.statements
Collection ;
// Add the method node to hello In the method blockStatement.statements.addAll(methods)
2、 Use new AstBuilder().buildFromString Conduct method injection
// 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)
3、 Use new AstBuilder().buildFromCode Conduct method injection
// 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)
Two 、 Complete code examples and the compilation process of compile time processing
1、Groovy Script Groovy.groovy
class Student{
def name def hello(){
println "hello" }}def student = new Student()student.hello()
2、ASTTransformation Interface implementation MyASTTransformation.groovy
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) {
println nodes println source println source.AST println source.source.reader.text // 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、 To configure ASTTransformation
establish D:\002_Project\012_Groovy\Groovy_Demo\src\main\groovy\resources\META-INF\servicesorg.codehaus.groovy.transform.ASTTransformation
Directory level and documents , Configure... In a file ASTTransformation
The full class name of the implementation class :
MyASTTransformation
3、 Use the command line for compile time processing
First , Get into D:\002_Project\012_Groovy\Groovy_Demo\src\main\groovy
Catalog ,
cd D:\002_Project\012_Groovy\Groovy_Demo\src\main\groovy
then , compile Compile time processing class MyASTTransformation.groovy , The compiled bytecode file MyASTTransformation.class
Save to D:\002_Project\012_Groovy\Groovy_Demo\src\main\groovy\classes
Under the table of contents ,
groovyc -d classes MyASTTransformation.groovy
And then , Package the above compiled bytecode file , Store in D:\002_Project\012_Groovy\Groovy_Demo\src\main\groovy\test.jar
route ;
jar -cf test.jar -C classes . -C resources .
Last , rely on test.jar perform Groovy.groovy Script
groovy -classpath test.jar Groovy.groovy
The execution result is :
[org.codehaus.groovy.ast.ModuleNode@7d7758be]org.codehaus.groovy.control.SourceUnit@2bdd8394org.codehaus.groovy.ast.ModuleNode@7d7758beclass Student{
def name def hello(){
println "hello" }}def student = new Student()student.hello()[org.codehaus.groovy.ast.ModuleNode@16ce702d]org.codehaus.groovy.control.SourceUnit@7b94089borg.codehaus.groovy.ast.ModuleNode@16ce702dprintln "hello buildFromString"[org.codehaus.groovy.ast.ModuleNode@72c28d64]org.codehaus.groovy.control.SourceUnit@6492fab5org.codehaus.groovy.ast.ModuleNode@72c28d64__synthesized__label__1644323893072__:{
println "hello buildFromCode" }hello buildFromCode
边栏推荐
- Analysis of the combination of small program technology advantages and industrial Internet
- 小程序技术优势与产业互联网相结合的分析
- Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
- AtCoder Beginner Contest 258【比赛记录】
- Hudi of data Lake (1): introduction to Hudi
- [designmode] Decorator Pattern
- 电机的简介
- NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
- The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
- Spark AQE
猜你喜欢
小程序技术优势与产业互联网相结合的分析
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
MYSQL GROUP_ The concat function realizes the content merging of the same ID
常用API类及异常体系
Key structure of ffmpeg - avframe
2022-02-13 work record -- PHP parsing rich text
如何制作自己的機器人
OS i/o devices and device controllers
Spark SQL null value, Nan judgment and processing
Calculate sha256 value of data or file based on crypto++
随机推荐
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
uniapp开发,打包成H5部署到服务器
Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
FFmpeg抓取RTSP图像进行图像分析
notepad++正则表达式替换字符串
OS i/o devices and device controllers
synchronized 和 ReentrantLock
Leetcode 44 Wildcard matching (2022.02.13)
NLP generation model 2017: Why are those in transformer
Extension and application of timestamp
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
详细页返回列表保留原来滚动条所在位置
The relationship between FPGA internal hardware structure and code
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
STM32按键消抖——入门状态机思维
MySQL functions
Knowledge about the memory size occupied by the structure
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
AtCoder Beginner Contest 254【VP记录】