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

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

文章目录

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

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

三、完整代码示例





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


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


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


class Student{
    def name;
    def hello() {
        System.out.println "Hello ${name}"
    }
}
def student = new Student(name: "Tom")


覆盖 student.metaClass 的 invokeMethod 方法 ,


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


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






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


使用


student.metaClass.invokeMethod = {}


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


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



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


首先 , 要从 student.metaClass 中根据 方法名 和 方法参数 获取指定的 MetaMethod ;

   

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


然后 , 执行该 MetaMethod 方法 , 需要传入 对象 和 参数 ;

   

// 方法不为空再执行该方法
        if (method != null) {
            method.invoke(student, args)
        }






三、完整代码示例


完整代码示例 :


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()



执行结果 :


invokeMethod : String name : hello , Object args : []
Hello Tom
Hello Jerry

image.png

目录
相关文章
|
Java
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )
117 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
139 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
144 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 使用 MetaClass 进行方法拦截 | 对象上拦截方法 | 类上拦截方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 使用 MetaClass 进行方法拦截 | 对象上拦截方法 | 类上拦截方法 )
153 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 使用 MetaClass 进行方法拦截 | 对象上拦截方法 | 类上拦截方法 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
170 0
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
116 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入普通方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
113 0
【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 注入静态方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 注入静态方法 )
115 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 注入静态方法 )
【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )
【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )
166 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入构造方法 )
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入构造方法 )
104 0
【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 MetaClass 进行方法注入构造方法 )