zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

2023-09-14 09:07:29 时间





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



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

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


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


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


被拦截方法原型 :

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 方法 , 查询字符串中是否包含指定的子串 ;

def string = "Hello World"

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

执行结果 :

true

3、拦截 String 对象的 contains 函数


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


代码示例 :

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

执行结果 :

Is "Hello World" contains "Hello"
true

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


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

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

执行结果 :

invokeMethod : String name : contains , Object args : [Hello]
true