【Groovy】编译时元编程 ( 编译时处理 ASTTransformation 接口实现 | 配置 ASTTransformation )
文章目錄
- 一、編譯時處理 ASTTransformation 接口實現
- 二、配置 ASTTransformation
一、編譯時處理 ASTTransformation 接口實現
聲明 ASTTransformation 接口子類 , 并使用 @GroovyASTTransformation 注解修飾該類 ; 在實現的 void visit(ASTNode[] nodes, SourceUnit source) 方法中進行編譯時處理 ;
import org.codehaus.groovy.ast.ASTNode import org.codehaus.groovy.control.SourceUnit import org.codehaus.groovy.transform.ASTTransformation import org.codehaus.groovy.transform.GroovyASTTransformation@GroovyASTTransformation class MyASTTransformation implements ASTTransformation {/*** 編譯時處理方法* @param nodes AST 抽象語法樹節點 , 是 ASTNode 數組類型* @param source 源單元 , 可以通過該對象拿到源文件*/@Overridevoid visit(ASTNode[] nodes, SourceUnit source) {println nodesprintln source} }二、配置 ASTTransformation
在 Y:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy\ 目錄下 , 創建 resources\META-INF\services\ 目錄層級 , 然后在 Y:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy\resources\META-INF\services 目錄下創建 org.codehaus.groovy.transform.ASTTransformation 文件 , 注意在 org.codehaus.groovy.transform.ASTTransformation 文件中寫上 ASTTransformation 實現類的全類名 , 此處沒有創建包 , 因此全類名是
MyASTTransformationY:\002_WorkSpace\003_IDEA\Groovy_Demo2\src\main\groovy\resources\META-INF\services\org.codehaus.groovy.transform.ASTTransformation 文件內容如下 :
總結
以上是生活随笔為你收集整理的【Groovy】编译时元编程 ( 编译时处理 ASTTransformation 接口实现 | 配置 ASTTransformation )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Groovy】编译时元编程 ( AST
- 下一篇: 【Groovy】编译时元编程 ( 编译