欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/6/17 编程问答 31 豆豆

文章目录

  • 一、重写 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

总结

以上是生活随笔为你收集整理的【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。