【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )
文章目录
- 一、GroovyInterceptable 接口简介
- 二、重写 GroovyObject#invokeMethod 方法
- 三、GroovyInterceptable 接口拦截效果
- 四、完整代码示例
一、GroovyInterceptable 接口简介
定义 Groovy 类时 , 令该类实现 GroovyInterceptable 接口 , 该 GroovyInterceptable 接口继承自 GroovyObject 接口 ,
public interface GroovyInterceptable extends GroovyObject {}
由上面的代码可知 , 在 GroovyInterceptable 接口中 , 没有在 GroovyObject 接口 的基础上 , 定义新的抽象方法 ;
二、重写 GroovyObject#invokeMethod 方法
定义 Student 实现 GroovyInterceptable 接口 ,
class Student implements GroovyInterceptable{def name;def hello() {println "Hello ${name}"}
}
那么调用 Student 对象的任何方法 , 都会调用到 GroovyObject 的 invokeMethod 方法 ;
public interface GroovyObject {/*** Invokes the given method.** @param name the name of the method to call* @param args the arguments to use for the method call* @return the result of invoking the method*/Object invokeMethod(String name, Object args);
}
重写 Student 类中的 invokeMethod 方法 ,
class Student implements GroovyInterceptable{def name;def hello() {println "Hello ${name}"}@OverrideObject invokeMethod(String name, Object args) {System.out.println "invokeMethod"}
}
三、GroovyInterceptable 接口拦截效果
GroovyInterceptable 接口 :
没有实现 GroovyInterceptable 接口 :
- 直接调用方法 : 不会触发 invokeMethod 方法 ;
- 通过 invokeMethod 调用方法 : 会触发 invokeMethod 方法 ;
- 调用不存在的方法 : 会报错 ;
实现了 GroovyInterceptable 接口 :
- 直接调用方法 : 会触发 invokeMethod 方法 ;
- 通过 invokeMethod 调用方法 : 会触发 invokeMethod 方法 ;
- 调用不存在的方法 : 不会报错 ;
四、完整代码示例
完整代码示例 :
class Student implements GroovyInterceptable{def name;def hello() {println "Hello ${name}"}@OverrideObject invokeMethod(String name, Object args) {System.out.println "invokeMethod : $name"}
}def student = new Student(name: "Tom")// 直接调用 hello 方法
student.hello()// 调用不存在的方法 , 也会触发 invokeMethod 方法
student.hello1()
执行结果 :
invokeMethod : hello
invokeMethod : hello1
【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 实现 GroovyInterceptable 接口 | 重写 invokeMethod 方法 )相关推荐
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法实现函数拦截 | 实现函数调用转发 )
文章目录 一.重写 MetaClass#invokeMethod 方法实现函数拦截 二.在 MetaClass#invokeMethod 方法中调用对象的其它方法 三.完整代码示例 一.重写 Meta ...
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )
文章目录 一.重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 1.被拦截的 String#contains 方法原型 2.JDK 正常用法 3.拦截 Strin ...
- 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )
文章目录 一.批量方法委托 二.完整代码示例 一.批量方法委托 在上一篇博客 [Groovy]MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 ) 中 , 将 ...
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Mixin 注解进行方法注入 | Mixin 混合多个类优先级分析 )
文章目录 一.使用 Mixin 混合进行方法注入 二.Mixin 混合多个类优先级分析 一.使用 Mixin 混合进行方法注入 在上一篇博客 [Groovy]MOP 元对象协议与元编程 ( 方法注入 ...
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )
文章目录 一.使用 ExpandoMetaClass 进行方法注入 三.完整代码示例 一.使用 ExpandoMetaClass 进行方法注入 在 [Groovy]MOP 元对象协议与元编程 ( 方法 ...
- 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类进行方法注入的优缺点 )
文章目录 一.使用 Category 分类进行方法注入的优点 二.使用 Category 分类进行方法注入的缺点 一.使用 Category 分类进行方法注入的优点 之前的博客 [Groovy]MOP ...
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 使用 MetaClass 进行方法拦截 | 对象上拦截方法 | 类上拦截方法 )
文章目录 一.使用 MetaClass 进行方法拦截 1.使用 MetaClass 在单个对象上进行方法拦截 2.使用 MetaClass 在类上进行方法拦截 二.完整代码示例 1.对象方法拦截 2. ...
- 【Groovy】MOP 元对象协议与元编程 ( 通过 MetaMethod#invoke 执行 Groovy 方法 )
文章目录 一.通过 MetaMethod#invoke 执行 Groovy 方法 二.完整代码示例 一.通过 MetaMethod#invoke 执行 Groovy 方法 已经定义 Groovy 类 ...
- 【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )
文章目录 一.Groovy 类内部和外部分别获取 metaClass 二.分析 Groovy 类内部和外部获取 metaClass 操作的字节码 三.使用 HandleMetaClass 注入方法 一 ...
最新文章
- Spring Boot实战pdf
- confluence安装_Hive安装
- ThinkPHP5显示数据库字段内容
- CentOS7设置自定义开机启动,添加自定义系统服务
- java在原数组中追加一个元素
- 微型计算机的字节取决于什么的宽度,计算机的字长取决于什么?
- 使用fiddler脚本修改x-frame-options
- NumPy学习笔记之zeros_like()函数(包含zeros函数)
- Python 在图片加上消息通知的文字
- 永远跳票的 永远的毁灭公爵
- 使用BarTender连接Excel打印标签
- 百度网盘免费高速下载(详细过程)
- 阿里云域名搭建DNSLOG
- Siebel命令行修改LDAP
- Word文档中如何添加带打勾的方框
- 指数的增长和衰退问题
- 思维导图 基础篇(11)应用-文章分析-框架法
- 【经验】不摸鱼的时光
- altium designer 快捷键
- 国产系统-Deepin安装图文(VIP典藏2022版)