文章目录

  • 一、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 方法 )相关推荐

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

    文章目录 一.重写 MetaClass#invokeMethod 方法实现函数拦截 二.在 MetaClass#invokeMethod 方法中调用对象的其它方法 三.完整代码示例 一.重写 Meta ...

  2. 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )

    文章目录 一.重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 1.被拦截的 String#contains 方法原型 2.JDK 正常用法 3.拦截 Strin ...

  3. 【Groovy】MOP 元对象协议与元编程 ( 方法委托 | 批量方法委托 )

    文章目录 一.批量方法委托 二.完整代码示例 一.批量方法委托 在上一篇博客 [Groovy]MOP 元对象协议与元编程 ( 方法委托 | 正常方法调用 | 方法委托实现 | 代码示例 ) 中 , 将 ...

  4. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 @Mixin 注解进行方法注入 | Mixin 混合多个类优先级分析 )

    文章目录 一.使用 Mixin 混合进行方法注入 二.Mixin 混合多个类优先级分析 一.使用 Mixin 混合进行方法注入 在上一篇博客 [Groovy]MOP 元对象协议与元编程 ( 方法注入 ...

  5. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 ExpandoMetaClass 进行方法注入 )

    文章目录 一.使用 ExpandoMetaClass 进行方法注入 三.完整代码示例 一.使用 ExpandoMetaClass 进行方法注入 在 [Groovy]MOP 元对象协议与元编程 ( 方法 ...

  6. 【Groovy】MOP 元对象协议与元编程 ( 方法注入 | 使用 Category 分类进行方法注入的优缺点 )

    文章目录 一.使用 Category 分类进行方法注入的优点 二.使用 Category 分类进行方法注入的缺点 一.使用 Category 分类进行方法注入的优点 之前的博客 [Groovy]MOP ...

  7. 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 使用 MetaClass 进行方法拦截 | 对象上拦截方法 | 类上拦截方法 )

    文章目录 一.使用 MetaClass 进行方法拦截 1.使用 MetaClass 在单个对象上进行方法拦截 2.使用 MetaClass 在类上进行方法拦截 二.完整代码示例 1.对象方法拦截 2. ...

  8. 【Groovy】MOP 元对象协议与元编程 ( 通过 MetaMethod#invoke 执行 Groovy 方法 )

    文章目录 一.通过 MetaMethod#invoke 执行 Groovy 方法 二.完整代码示例 一.通过 MetaMethod#invoke 执行 Groovy 方法 已经定义 Groovy 类 ...

  9. 【Groovy】MOP 元对象协议与元编程 ( Groovy 类内部和外部分别获取 metaClass | 分析获取 metaClass 操作的字节码 | HandleMetaClass 注入方法 )

    文章目录 一.Groovy 类内部和外部分别获取 metaClass 二.分析 Groovy 类内部和外部获取 metaClass 操作的字节码 三.使用 HandleMetaClass 注入方法 一 ...

最新文章

  1. Spring Boot实战pdf
  2. confluence安装_Hive安装
  3. ThinkPHP5显示数据库字段内容
  4. CentOS7设置自定义开机启动,添加自定义系统服务
  5. java在原数组中追加一个元素
  6. 微型计算机的字节取决于什么的宽度,计算机的字长取决于什么?
  7. 使用fiddler脚本修改x-frame-options
  8. NumPy学习笔记之zeros_like()函数(包含zeros函数)
  9. Python 在图片加上消息通知的文字
  10. 永远跳票的 永远的毁灭公爵
  11. 使用BarTender连接Excel打印标签
  12. 百度网盘免费高速下载(详细过程)
  13. 阿里云域名搭建DNSLOG
  14. Siebel命令行修改LDAP
  15. Word文档中如何添加带打勾的方框
  16. 指数的增长和衰退问题
  17. 思维导图 基础篇(11)应用-文章分析-框架法
  18. 【经验】不摸鱼的时光
  19. altium designer 快捷键
  20. 国产系统-Deepin安装图文(VIP典藏2022版)

热门文章

  1. php中的__autoload()函数
  2. IOS判断用户的网络类型(2/3/4G、wifi)
  3. Nginx版本无缝升级
  4. 用JSON技术加快AJAX程序开发
  5. SQL必知必会——插入数据(十五)
  6. python常用模块之shelve模块
  7. IntelliTest(5) - The IntelliTest Reference Manual[译]
  8. ubuntu系统安装和配置
  9. 演化计算简单实例(附代码)
  10. Sqlite c/c++ api 学习