前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数 )

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

作者头像
韩曙亮
发布2023-03-30 10:39:25
6210
发布2023-03-30 10:39:25
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数


重写 MetaClass#invokeMethod 方法 , 不仅可以拦截自定义的类中的方法 , 还可以拦截 JDK 中已经定义完毕的方法 ;

如果要拦截 JDK 中的方法 , 肯定不能使用 实现 GroovyInterceptable 接口的方法 , 只能使用重写 MetaClass#invokeMethod 方法进行拦截 ;

此处以 String 类为例 , 拦截其中的 contains 方法 , 查询 String 常量 “Hello World” 中是否包含某个子串 “Hello” ;

1、被拦截的 String#contains 方法原型

被拦截方法原型 :

代码语言:javascript
复制
public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /**
     * 当且仅当此字符串包含指定的字符值序列时,返回true。
     *
     * @param s 要查找的字符串
     * @return 如果字符串中包含要查找的字符串返回 true , 反之返回 false
     * @since 1.5
     */
    public boolean contains(CharSequence s) {
        return indexOf(s.toString()) > -1;
    }
}

2、JDK 正常用法

正常用法 : 使用 JDK 中的 String 类中定义的 contains 方法 , 查询字符串中是否包含指定的子串 ;

代码语言:javascript
复制
def string = "Hello World"

// 查询字符串中是否包含 "Hello" 字符串
def flag = string.contains("Hello")
println flag

执行结果 :

代码语言:javascript
复制
true

3、拦截 String 对象的 contains 函数

为 string.metaClass.contains 赋值一个闭包 , 在闭包中接收 CharSequence s 参数 , 这个参数就是传入的要查找的子串 ;

代码示例 :

代码语言:javascript
复制
def string = "Hello World"

string.metaClass.contains = {
    CharSequence s->
        System.out.println "Is \"$string\" contains \"$s\""
        true
}


// 查询字符串中是否包含 "Hello" 字符串
def flag = string.contains("Hello")
println flag

执行结果 :

代码语言:javascript
复制
Is "Hello World" contains "Hello"
true

4、重写 MetaClass#invokeMethod 方法进行函数拦截

使用下面的方法可以拦截所有的函数 ;

代码语言:javascript
复制
def string = "Hello World"

string.metaClass.invokeMethod = {
    String name, Object args ->
        System.out.println "invokeMethod : Object : $string , Method name : $name , Object args : $args"

        // 方法转发 : 调用 string 对象中的原来的方法
        // 注意此处不能使用 metaClass.invokeMethod 方法调用对象中的方法 , 会导致栈溢出
        // 这里通过 MetaClass#getMetaMethod 获取方法 , 然后执行
        def method = delegate.metaClass.getMetaMethod(name, args)
        // 方法不为空再执行该方法
        if (method != null) {
            method.invoke(delegate, args)
        }
}


// 查询字符串中是否包含 "Hello" 字符串
def flag = string.contains("Hello")
println flag

执行结果 :

代码语言:javascript
复制
invokeMethod : String name : contains , Object args : [Hello]
true
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、重写 MetaClass#invokeMethod 方法拦截 JDK 中已经定义的函数
    • 1、被拦截的 String#contains 方法原型
      • 2、JDK 正常用法
        • 3、拦截 String 对象的 contains 函数
          • 4、重写 MetaClass#invokeMethod 方法进行函数拦截
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档