前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )

【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )

作者头像
韩曙亮
发布2023-03-30 10:39:09
3950
发布2023-03-30 10:39:09
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、重写 MetaClass#invokeMethod 方法实现函数拦截


在 Groovy 中 , 如果覆盖了对象的 MetaClass#invokeMethod 方法 , 那么 , 在执行该对象的任何方法时 , 都会回调该 invokeMethod 方法 ;

给定一个类和该类的实例对象 :

代码语言:javascript
复制
class Student{

    def name;

    def hello() {
        System.out.println "Hello ${name}"
    }
}

def student = new Student(name: "Tom")

覆盖 student.metaClass 的 invokeMethod 方法 ,

代码语言:javascript
复制
// 如果覆盖了 invokeMethod 方法
// 那么 , 执行该对象的任何方法时 , 都会回调该 invokeMethod 方法
student.metaClass.invokeMethod = {
    String name, Object args ->
        System.out.println "invokeMethod : String name : $name , Object args : $args"
}

调用 student 对象的 hello 方法时 , 就会回调该闭包中的方法 , 即使没有实现 GroovyInterceptable 接口 , 也可以进行函数拦截 ;

二、在 MetaClass#invokeMethod 方法中调用对象的其它方法


使用

代码语言:javascript
复制
student.metaClass.invokeMethod = {}

重写了 invokeMethod 方法后 , 拦截函数之后 , 需要将方法传递下去 , 调用真正要调用的方法 ;

注意此处不能使用 student.metaClass.invokeMethod 调用其它方法 , 这样会导致无限循环递归调用 , 导致栈溢出异常 ;

在 MetaClass#invokeMethod 方法中调用对象的其它方法 ,

  • 首先 , 要从 student.metaClass 中根据 方法名 和 方法参数 获取指定的 MetaMethod ;
代码语言:javascript
复制
        // 方法转发 : 调用 student 对象中的原来的方法
        // 注意此处不能使用 metaClass.invokeMethod 方法调用对象中的方法 , 会导致栈溢出
        // 这里通过 MetaClass#getMetaMethod 获取方法 , 然后执行
        def method = student.metaClass.getMetaMethod(name, args)
  • 然后 , 执行该 MetaMethod 方法 , 需要传入 对象 和 参数 ;
代码语言:javascript
复制
        // 方法不为空再执行该方法
        if (method != null) {
            method.invoke(student, args)
        }

三、完整代码示例


完整代码示例 :

代码语言:javascript
复制
class Student{

    def name;

    def hello() {
        System.out.println "Hello ${name}"
    }
}

def student = new Student(name: "Tom")
def student2 = new Student(name: "Jerry")

// 如果覆盖了 invokeMethod 方法
// 那么 , 执行该对象的任何方法时 , 都会回调该 invokeMethod 方法
student.metaClass.invokeMethod = {
    String name, Object args ->
        System.out.println "invokeMethod : String name : $name , Object args : $args"

        // 方法转发 : 调用 student 对象中的原来的方法
        // 注意此处不能使用 metaClass.invokeMethod 方法调用对象中的方法 , 会导致栈溢出
        // 这里通过 MetaClass#getMetaMethod 获取方法 , 然后执行
        def method = student.metaClass.getMetaMethod(name, args)
        // 方法不为空再执行该方法
        if (method != null) {
            method.invoke(student, args)
        }
}

// 直接调用 hello 方法
student.hello()
student2.hello()

执行结果 :

代码语言:javascript
复制
invokeMethod : String name : hello , Object args : []
Hello Tom
Hello Jerry
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、重写 MetaClass#invokeMethod 方法实现函数拦截
  • 二、在 MetaClass#invokeMethod 方法中调用对象的其它方法
  • 三、完整代码示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档