如何在groovy中为类添加新的闭包

时间:2009-10-29 12:13:32

标签: groovy metaprogramming metaclass expandometaclass

来自Snipplr

这里是脚本代码,在评论中是问题和引发的异常

class Class1 {
    def closure = {
        println this.class.name
        println delegate.class.name
        def nestedClos = {
            println owner.class.name
        }
        nestedClos()
    }
}

def clos = new Class1().closure
clos.delegate = this
clos()

//Now I want to add a new closure to Class1

def newClosure = {
    println "new Closure"
    println this.class.name
    println delegate.class.name
    def nestedClos = {
        println owner.class.name
    }
    nestedClos()
}

//getAbc to create a property, not a method
Class1.metaClass.getAbc = newClosure

//What happens here is that the property abc is not used as a closure per se, it's used
//as a property and when I execute it just run the closure and when I want to change
//the delegate, a null pointer is thrown
clos = new Class1().abc //abc executed instead of passing the reference closure
clos.delegate = this  //Exception!!!!
clos()

1 个答案:

答案 0 :(得分:0)

好的,这样做并不是一种奇特的方式,但我有解决方案......是的!

将属性创建为对象,然后分配闭包

class Class1 {
    def closure = {
        println this.class.name
        println delegate.class.name
        def nestedClos = {
            println owner.class.name
        }
        nestedClos()
    }
}

def clos = new Class1().closure
clos.delegate = this
clos()

//Now I want to add a new closure to Class1

def newClosure = {
    println "new Closure"
    println this.class.name
    println delegate.class.name
    def nestedClos = {
        println owner.class.name
    }
    nestedClos()
}

//before edit
//Class1.metaClass.abc = new Object()
Class1.metaClass.abc = newClosure    


def cl = new Class1()
//Before edit
//For the sake of simplicity we are going to use & for the method
//clos = cl.abc 
closs = cl.&abc
clos.delegate = this
clos()