zl程序教程

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

当前栏目

【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 通过 MetaClass#invokeMethod 方法调用类其它方法 )

方法对象编程协议 函数 进行 通过 调用
2023-09-14 09:07:29 时间





一、通过 MetaClass#invokeMethod 方法调用类其它方法



注意在 invokeMethod 方法中 , 不能调用 invokeMethod 方法 , 这样调用肯定会出现无限循环递归 , 导致栈溢出 ;

此处只能通过调用 MetaClass#invokeMethod 方法 , 调用相关函数 ;

通过元类对象的 invokeMethod 方法 , 不会导致栈溢出 ;


获取该 Groovy 类的 metaClass , 然后调用 metaClass 的 invokeMethod 方法 , 传入调用对象 , 调用方法 , 方法参数 , 即可调用相关方法 ;

        // 检查该类中是否定义 name 方法 , 参数是 args
        def hasMethod = metaClass.invokeMethod(this, "respondsTo", name, args)

传入了的方法名 , 如果存在 , 则直接通过 metaClass.invokeMethod 调用该方法 ;

        // 如果定义了该方法 , 则执行该方法
        if (hasMethod) {
            metaClass.invokeMethod(this, name, args)
        }




二、完整代码示例



完整代码示例 :

class Student implements GroovyInterceptable{
    def name;

    def hello() {
        System.out.println "Hello ${name}"
    }

    @Override
    Object invokeMethod(String name, Object args) {
        System.out.println "invokeMethod : $name"

        /*
            注意在 invokeMethod 方法中 , 不能调用 invokeMethod 方法
            肯定会出现递归调用 , 导致栈溢出
            只能通过调用 MetaClass#invokeMethod 方法 , 调用相关函数
         */

        // 检查该类中是否定义 name 方法 , 参数是 args
        def hasMethod = metaClass.invokeMethod(this, "respondsTo", name, args)

        /*
            注意这里区分 Student 中的 invokeMethod 方法 与
            MetaClass 中的 invokeMethod 方法
         */

        // 如果定义了该方法 , 则执行该方法
        if (hasMethod) {
            metaClass.invokeMethod(this, name, args)
        }
    }
}

def student = new Student(name: "Tom")

// 直接调用 hello 方法
student.hello()

// 调用不存在的方法 , 也会触发 invokeMethod 方法
student.hello1()

// 通过 GroovyObject#invokeMethod 调用 hello 方法
// 第二个参数是函数参数 , 如果为 void 则传入 null
//student.invokeMethod("hello", null)

// 获取 元方法
//MetaMethod metaMethod = student.getMetaClass().getMetaMethod("hello", null)
// 执行元方法
//metaMethod.invoke(student, null)

执行结果 :

invokeMethod : hello
Hello Tom
invokeMethod : hello1

在这里插入图片描述