当前位置:网站首页>[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
2022-07-06 00:35:00 【Programmer community】
List of articles
- One 、 stay MyASTTransformation#visit Method interception in method
- 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 interception in method
In the last blog 【Groovy】 Compile time metaprogramming ( Compile time method interception | stay MyASTTransformation#visit Method to intercept ) in , stay MyASTTransformation#visit In the method , First of all, I got ModuleNode , Then find the specified ClassNode , From the specified ClassNode Find the corresponding MethodNode node ;
Method interception is mainly in MethodNode Node ; The MethodNode The node corresponding to the Student Class hello Method ;
according to 【Groovy】 Compile time metaprogramming ( Method used in interception AST Syntax tree node MethodNode node | MethodNode Node analysis | MethodNode Nodes in the BlockStatement) Blog analysis , MethodNode The node encapsulates private Statement code;
member , The code The actual type of member is BlockStatement type ;
public class BlockStatement extends Statement
First of all, I will MethodNode Node code Members are assigned to BlockStatement , Is equivalent to Statement Object to BlockStatement type ;
BlockStatement blockStatement = code
stay BlockStatement In the object , Encapsulates the private List<Statement> statements = new ArrayList<Statement>();
member , This is the MethodNode Method content in the node , Empty the collection , It is equivalent to emptying the method body , The method interception operation is implemented ;
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() } }}
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()
边栏推荐
- Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
- 如何制作自己的機器人
- Spark DF增加一列
- FFT learning notes (I think it is detailed)
- 电机的简介
- 【DesignMode】适配器模式(adapter pattern)
- Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
- Codeforces Round #804 (Div. 2)【比赛记录】
- Idea remotely submits spark tasks to the yarn cluster
- 2022-02-13 work record -- PHP parsing rich text
猜你喜欢
[designmode] composite mode
Hardware and interface learning summary
How to use the flutter framework to develop and run small programs
Notepad++ regular expression replacement string
看抖音直播Beyond演唱会有感
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
MySql——CRUD
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
Spark SQL null value, Nan judgment and processing
随机推荐
uniapp开发,打包成H5部署到服务器
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
常用API类及异常体系
Classical concurrency problem: the dining problem of philosophers
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
Uniapp development, packaged as H5 and deployed to the server
数据分析思维分析方法和业务知识——分析方法(二)
Single source shortest path exercise (I)
Key structure of ffmpeg - avformatcontext
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL存储引擎
Idea远程提交spark任务到yarn集群
Extension and application of timestamp
Spark DF adds a column
AtCoder Beginner Contest 258【比赛记录】
MySql——CRUD
Ffmpeg captures RTSP images for image analysis
Spark SQL null value, Nan judgment and processing
如何利用Flutter框架开发运行小程序