【Groovy】编译时元编程 ( 编译时方法拦截 | 在 MyASTTransformation#visit 方法中找到要拦截的方法 )

简介: 【Groovy】编译时元编程 ( 编译时方法拦截 | 在 MyASTTransformation#visit 方法中找到要拦截的方法 )

一、在 MyASTTransformation#visit 方法中找到要拦截的方法


在 ASTTransformation 接口实现类的


void visit(ASTNode[] nodes, SourceUnit source)


方法中 , 其中 ASTNode[] nodes 参数是 AST 语法树根节点数组 , 每个数组元素都是一个 ModuleNode 对应一个 Groovy 脚本 ;


SourceUnit source 是源单元 , 可以通过该对象拿到源文件 ;


source.AST 是单个 ModuleNode 对象 , 对应一个 Groovy 脚本 ;



1、获取 ClassNode 节点集合


source.AST.classes 就是一个 Groovy 脚本中定义的类节点数组 ; 这是在 ModuleNode 中的 ClassNode 类节点封装在了 List classes = new LinkedList(); 成员中 ;



2、查找指定的 ClassNode 节点


使用


   

source.AST.classes.find {
            // 查找名称为 Student 的类
            // it 是 ClassNode 节点
            it.name == "Student"
        }


代码 , 可以查找到名称为 “Student” 的 ClassNode 节点 , 也就是 Student 类对应的节点 ;


集合的 find 方法原型如下 , 得到的是一个集合元素对象 ; 该方法返回的是集合中第一个与闭包条件匹配的集合元素 ;



 

/**
     * 查找与闭包条件匹配的第一个值。例子:
     * <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、获取指定 ClassNode 节点下的 MethodNode 节点集合


再进一步 , 如果获取的 ClassNode 节点不为空 , 则获取该节点下的 MethodNode 节点集合 , 使用 ?.methods 代码获取 ,


   

source.AST.classes.find {
            // 查找名称为 Student 的类
            // it 是 ClassNode 节点
            it.name == "Student"
        }?.methods


ClassNode 的 getMethods 方法原型如下 :


public class ClassNode extends AnnotatedNode implements Opcodes {
    /**
     * @return 与此相关的方法 {@code ClassNode}
     */
    public List<MethodNode> getMethods() {
        if (redirect != null)
            return redirect.getMethods();
        lazyClassInit();
        return methodsList;
    }
}



4、查找指定的 MethodNode 节点


查找 List 集合中 , 名称为 “hello” 的节点 , 也就是查找 Student 类中的 hello 方法对应的 MethodNode 节点 ;


 

source.AST.classes.find {
            // 查找名称为 Student 的类
            // it 是 ClassNode 节点
            it.name == "Student"
        }?.methods?.find {
            // 查找 Student 类下名称为 hello 的方法
            // it 是 MethodNode 节点
            it.name == "hello"
        }
目录
相关文章
Grafana实现图表双Y坐标轴展示
Grafana实现图表双Y坐标轴展示
|
存储 文件存储
如何使用Nest.js 上传文件及自定义文件名保存
在 Nest.js 中进行文件上传并自定义文件名保存的过程相对简单
734 0
|
SQL 存储 Oracle
6 张图带你彻底搞懂分布式事务 XA 模式
XA 协议是由 X/Open 组织提出的分布式事务处理规范,主要定义了事务管理器 TM 和局部资源管理器 RM 之间的接口。目前主流的数据库,比如 oracle、DB2 都是支持 XA 协议的。
13653 1
6 张图带你彻底搞懂分布式事务 XA 模式
|
11月前
|
存储 人工智能 算法
卷起来!让智能体评估智能体,Meta发布Agent-as-a-Judge
Meta(原Facebook)提出了一种名为Agent-as-a-Judge的框架,用于评估智能体的性能。该框架包含八个模块,通过构建项目结构图、定位相关文件、读取多格式数据、搜索和检索信息、询问要求满足情况、存储历史判断、以及规划下一步行动,有效提升了评估的准确性和稳定性。实验结果显示,Agent-as-a-Judge在处理复杂任务依赖关系方面优于大型语言模型,但在资源消耗和潜在偏见方面仍面临挑战。
379 1
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
|
存储 网络安全 开发工具
Git 协同开发详解:从基础命令到多人协作
Git 协同开发详解:从基础命令到多人协作
300 0
|
存储 PHP 数据安全/隐私保护
攻防世界 Web_php_unserialize
攻防世界 Web_php_unserialize
287 0
|
存储 SQL JSON
【编程语言】Groovy入门指南
【编程语言】Groovy入门指南
1017 0
|
SQL 关系型数据库 数据库
|
SQL 前端开发 JavaScript
基于java+springboot的视频点播网站-在线视频点播系统
该系统是基于java+springboot开发的视频点播系统。是给师妹开发的课程设计。
638 0