groovy.lang.GroovyInterceptable接口是扩展GroovyObject的标记接口,用于通知Groovy运行时应通过Groovy运行时的方法调度机制拦截所有方法。

    1. package groovy.lang;
    2. public interface GroovyInterceptable extends GroovyObject {
    3. }

    当Groovy对象实现GroovyInterceptable接口时,任何方法调用都会调用其invokeMethod()。下面是此类对象的简单示例:

    class Interception implements GroovyInterceptable {
    
        def definedMethod() { }
    
        def invokeMethod(String name, Object args) {
            'invokedMethod'
        }
    }
    

    下一段代码是一个测试,它表明对现有方法和不存在方法的调用都将返回相同的值。

    class InterceptableTest extends GroovyTestCase {
    
        void testCheckInterception() {
            def interception = new Interception()
    
            assert interception.definedMethod() == 'invokedMethod'
            assert interception.someMethod() == 'invokedMethod'
        }
    }