前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Groovy】编译时元编程 ( 编译时方法拦截 | 在 MyASTTransformation#visit 方法中找到要拦截的方法 )

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

作者头像
韩曙亮
发布2023-03-30 11:07:23
2850
发布2023-03-30 11:07:23
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

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


在 ASTTransformation 接口实现类的

代码语言:javascript
复制
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<ClassNode> classes = new LinkedList<ClassNode>(); 成员中 ;

2、查找指定的 ClassNode 节点

使用

代码语言:javascript
复制
        source.AST.classes.find {
            // 查找名称为 Student 的类
            // it 是 ClassNode 节点
            it.name == "Student"
        }

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

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

代码语言:javascript
复制
    /**
     * 查找与闭包条件匹配的第一个值。例子:
     * <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 代码获取 ,

代码语言:javascript
复制
        source.AST.classes.find {
            // 查找名称为 Student 的类
            // it 是 ClassNode 节点
            it.name == "Student"
        }?.methods

ClassNode 的 getMethods 方法原型如下 :

代码语言:javascript
复制
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<MethodNode> 集合中 , 名称为 “hello” 的节点 , 也就是查找 Student 类中的 hello 方法对应的 MethodNode 节点 ;

代码语言:javascript
复制
        source.AST.classes.find {
            // 查找名称为 Student 的类
            // it 是 ClassNode 节点
            it.name == "Student"
        }?.methods?.find {
            // 查找 Student 类下名称为 hello 的方法
            // it 是 MethodNode 节点
            it.name == "hello"
        }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、在 MyASTTransformation#visit 方法中找到要拦截的方法
    • 1、获取 ClassNode 节点集合
      • 2、查找指定的 ClassNode 节点
        • 3、获取指定 ClassNode 节点下的 MethodNode 节点集合
          • 4、查找指定的 MethodNode 节点
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档