【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )
2023-06-13 09:18:00 时间
文章目录
一、重写 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
相关文章
- es6数组和对象常用方法
- js的sort排序方法_sort对象排序
- 【原创】Java编程思想以及类,对象,属性,方法,包的解释
- 对象的新增方法
- 【C++ 语言】C++字符串 ( string 类 | 创建方法 | 控制台输出 | 字符串操作 | 栈内存字符串对象 | string* )
- 【Groovy】MOP 元对象协议与元编程 ( 通过 MetaMethod#invoke 执行 Groovy 方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
- 【Groovy】MOP 元对象协议与元编程 ( Expando 动态类 | 创建动态类 | 为动态类增加字段和方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法合成引入 | 类内部获取 HandleMetaClass )
- 【Groovy】MOP 元对象协议与元编程 ( 方法合成 | 动态注入方法 )
- 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 使用 @Delegate 注解进行方法委托 )
- 宝塔面板卸载方法
- 利用函数返回oracle对象表的三种方法
- 数学对象Math ceil()、floor()、round()方法详解编程语言
- Struts2 ServletActionContext.getResponse()方法:获取HttpServletResponse对象
- javascript解析后的xml对象的读取方法细解
- JavaScript中数组对象的那些自带方法介绍
- Android系列之Intent传递对象的几种实例方法
- ASP.NETASHX中获得Session的方法
- 图解修改IISweb.config配置文件增设置默认文档的方法
- javascripthistory对象(历史记录)使用方法(实现浏览器前进后退)
- Javascript中创建自定义对象的方法汇总
- 计算一个Java对象占用字节数的方法