[groovy-user] Change closures delegate results in no property exception

7 views
Skip to first unread message

Batman

unread,
Aug 20, 2014, 8:41:26 AM8/20/14
to us...@groovy.codehaus.org
I’m trying to play around with Groovy closures. Here is what I have:

class WaitLoaderConstrains{

def methodMissing(String methodName, args) {
if(methodName == "wait")
{
def a = args[0] as Map
println "Author is " + a['name']
}
}
}

class Author {

def name1 = "author name"

static constraints = {
wait name: name1 , time: 123
}
}

def a = new Author()

a.constraints.delegate = new WaitLoaderConstrains()


a.constraints()

but this sadly throws the exception:

groovy.lang.MissingPropertyException: No such property: name1 for class:
Author
Possible solutions: name1
at Author$__clinit__closure1.doCall(Script1.groovy:17)
at Author$__clinit__closure1.doCall(Script1.groovy)
at Script1.run(Script1.groovy:26)

why the closure’s can’t access, name1 from the same Author class?


Is it a bug or I'm missing something here?



--
View this message in context: http://groovy.329449.n5.nabble.com/Change-closures-delegate-results-in-no-property-exception-tp5720902.html
Sent from the groovy - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Dinko Srkoč

unread,
Aug 20, 2014, 9:19:23 AM8/20/14
to us...@groovy.codehaus.org
On 20 August 2014 14:39, Batman <t-ara...@juspay.in> wrote:
> I’m trying to play around with Groovy closures. Here is what I have:
>
> class WaitLoaderConstrains{
>
> def methodMissing(String methodName, args) {
> if(methodName == "wait")
> {
> def a = args[0] as Map
> println "Author is " + a['name']
> }
> }
> }
>
> class Author {
>
> def name1 = "author name"
>
> static constraints = {
> wait name: name1 , time: 123
> }
> }
>
> def a = new Author()
>
> a.constraints.delegate = new WaitLoaderConstrains()
>
>
> a.constraints()
>
> but this sadly throws the exception:
>
> groovy.lang.MissingPropertyException: No such property: name1 for class:
> Author
> Possible solutions: name1
> at Author$__clinit__closure1.doCall(Script1.groovy:17)
> at Author$__clinit__closure1.doCall(Script1.groovy)
> at Script1.run(Script1.groovy:26)
>
> why the closure’s can’t access, name1 from the same Author class?

`constraints` is a static field, `name1` is not. If you made `name1`
to also be a static field, it would work.

Cheers,
Dinko
Reply all
Reply to author
Forward
0 new messages