【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 ;
- 然后 , 执行该 MetaMethod 方法 , 需要传入 对象 和 参数 ;
三、完整代码示例
完整代码示例 :
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总结
以上是生活随笔为你收集整理的【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 【Groovy】MOP 元对象协议与元编
- 下一篇: 【Groovy】MOP 元对象协议与元编