当前位置:网站首页>[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
2022-07-06 00:35:00 【Programmer community】
List of articles
- One 、 stay MyASTTransformation#visit Method to intercept
- 1、 obtain ClassNode Node collection
- 2、 Find the specified ClassNode node
- 3、 Get specified ClassNode Node under MethodNode Node collection
- 4、 Find the specified MethodNode node
One 、 stay MyASTTransformation#visit Method to intercept
stay ASTTransformation Interface implementation class
void visit(ASTNode[] nodes, SourceUnit source)
In the method , among ASTNode[] nodes
Parameter is AST Syntax tree root node array , Each array element is a ModuleNode Corresponding to one Groovy Script ;
SourceUnit source
Is the source unit , You can get the source file through this object ;
source.AST
It's a single ModuleNode object , Corresponding to one Groovy Script ;
1、 obtain ClassNode Node collection
source.AST.classes
It's just one. Groovy The array of class nodes defined in the script ; This is ModuleNode Medium ClassNode Class nodes are encapsulated in List<ClassNode> classes = new LinkedList<ClassNode>();
Among members ;
2、 Find the specified ClassNode node
Use
source.AST.classes.find {
// The search name is Student Class // it yes ClassNode node it.name == "Student" }
Code , You can find the name “Student” Of ClassNode node , That is to say Student Class corresponding node ;
A collection of find The prototype of the method is as follows , What you get is a collection element object ; This method returns the first set element in the set that matches the closure condition ;
/** * Find the first value that matches the closure condition . Example : * <pre class="groovyTestCase">def list = [1,2,3] * assert 2 == list.find { it {@code >} 1 } * </pre> * * @param self a Collection * @param closure a closure condition * @return the first Object found, in the order of the collections iterator, or null if no element matches * @since 1.0 */ public static <T> T find(Collection<T> self, @ClosureParams(FirstParam.FirstGenericType.class) Closure closure) {
BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure); for (T value : self) {
if (bcw.call(value)) {
return value; } } return null; }
3、 Get specified ClassNode Node under MethodNode Node collection
Further , If you get ClassNode Node is not empty , Get MethodNode Node collection , Use ?.methods
Code acquisition ,
source.AST.classes.find {
// The search name is Student Class // it yes ClassNode node it.name == "Student" }?.methods
ClassNode Of getMethods The prototype of the method is as follows :
public class ClassNode extends AnnotatedNode implements Opcodes {
/** * @return Methods related to this {@code ClassNode} */ public List<MethodNode> getMethods() {
if (redirect != null) return redirect.getMethods(); lazyClassInit(); return methodsList; }}
4、 Find the specified MethodNode node
lookup List<MethodNode>
Collection , The name is “hello” The node of , That is to find Student Class hello Method corresponding to MethodNode node ;
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" }
边栏推荐
- Cloud guide DNS, knowledge popularization and classroom notes
- Extracting profile data from profile measurement
- Arduino六足机器人
- Room cannot create an SQLite connection to verify the queries
- 《编程之美》读书笔记
- Uniapp development, packaged as H5 and deployed to the server
- Ffmpeg learning - core module
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- [Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
- XML Configuration File
猜你喜欢
如何制作自己的机器人
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
免费的聊天机器人API
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
OS i/o devices and device controllers
AtCoder Beginner Contest 254【VP记录】
Set data real-time update during MDK debug
Key structure of ffmpeg - avformatcontext
Atcoder beginer contest 254 [VP record]
随机推荐
Leetcode 44 Wildcard matching (2022.02.13)
Model analysis of establishment time and holding time
Arduino hexapod robot
Idea远程提交spark任务到yarn集群
NLP generation model 2017: Why are those in transformer
建立时间和保持时间的模型分析
Tools to improve work efficiency: the idea of SQL batch generation tools
[designmode] Decorator Pattern
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
电机的简介
Set data real-time update during MDK debug
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Spark AQE
KDD 2022 | 脑电AI助力癫痫疾病诊断
FFmpeg抓取RTSP图像进行图像分析
时间戳的拓展及应用实例
FPGA内部硬件结构与代码的关系
Spark SQL空值Null,NaN判断和处理
小程序技术优势与产业互联网相结合的分析