Sudhir N | 26 Jun 2012 19:56
Picon
Favicon

[groovy-user] Overriding properties using metaClass won't work within class !


See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 
Wujek Srujek | 26 Jun 2012 20:21
Picon

Re: [groovy-user] Overriding properties using metaClass won't work within class !

Try this:


    String toString() {
        return "${getService()} $title"
    }

I get "hello null"

In your toString, it's not the properties that get called, but the fields directly.

wujek

On Tue, Jun 26, 2012 at 7:56 PM, Sudhir N <sudhir_nimavat-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:

See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 

Sudhir N | 26 Jun 2012 20:24
Picon
Favicon

Re: [groovy-user] Overriding properties using metaClass won't work within class !


so when the field is being referenced using the instance reference (b.service) groovy intelligently uses the metaClass property but not within the class.
Shouldn't this behavior be consistent ? 
 
   
Sudhir 
 

From: Wujek Srujek <wujek.srujek-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: user-i9PBDF1N6cxnkHa44VUL00B+6BGkLq7r@public.gmane.org
Sent: Tuesday, 26 June 2012 11:51 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !

Try this:

    String toString() {
        return "${getService()} $title"
    }

I get "hello null"

In your toString, it's not the properties that get called, but the fields directly.

wujek

On Tue, Jun 26, 2012 at 7:56 PM, Sudhir N <sudhir_nimavat-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:

See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 



Guillaume Laforge | 26 Jun 2012 20:29
Gravatar

Re: [groovy-user] Overriding properties using metaClass won't work within class !

Imagine you define your own getters:


class Person {
    private String name
    String getName() { name }
}

If we'd call getName() inside the class, String getName() { name } would actually mean String getName() { getName() } and you get a stack overflow error.


On Tue, Jun 26, 2012 at 8:24 PM, Sudhir N <sudhir_nimavat <at> yahoo.com> wrote:

so when the field is being referenced using the instance reference (b.service) groovy intelligently uses the metaClass property but not within the class.
Shouldn't this behavior be consistent ? 
 
   
Sudhir 
 

From: Wujek Srujek <wujek.srujek <at> gmail.com>
To: user <at> groovy.codehaus.org
Sent: Tuesday, 26 June 2012 11:51 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !

Try this:

    String toString() {
        return "${getService()} $title"
    }

I get "hello null"

In your toString, it's not the properties that get called, but the fields directly.

wujek

On Tue, Jun 26, 2012 at 7:56 PM, Sudhir N <sudhir_nimavat <at> yahoo.com> wrote:

See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 






--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one
Sudhir N | 26 Jun 2012 20:35
Picon
Favicon

Re: [groovy-user] Overriding properties using metaClass won't work within class !

Aah - I get it now - Thanks Guillaume  
   
Sudhir 
 

From: Guillaume Laforge <glaforge-yCVjj/EcxBJg9hUCZPvPmw@public.gmane.org>
To: user-i9PBDF1N6cxnkHa44VUL00B+6BGkLq7r@public.gmane.org
Sent: Tuesday, 26 June 2012 11:59 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !

Imagine you define your own getters:

class Person {
    private String name
    String getName() { name }
}

If we'd call getName() inside the class, String getName() { name } would actually mean String getName() { getName() } and you get a stack overflow error.


On Tue, Jun 26, 2012 at 8:24 PM, Sudhir N <sudhir_nimavat <at> yahoo.com> wrote:

so when the field is being referenced using the instance reference (b.service) groovy intelligently uses the metaClass property but not within the class.
Shouldn't this behavior be consistent ? 
 
   
Sudhir 
 

From: Wujek Srujek <wujek.srujek-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: user <at> groovy.codehaus.org
Sent: Tuesday, 26 June 2012 11:51 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !

Try this:

    String toString() {
        return "${getService()} $title"
    }

I get "hello null"

In your toString, it's not the properties that get called, but the fields directly.

wujek

On Tue, Jun 26, 2012 at 7:56 PM, Sudhir N <sudhir_nimavat-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:

See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 






--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one


Wujek Srujek | 26 Jun 2012 20:30
Picon

Re: [groovy-user] Overriding properties using metaClass won't work within class !

b.service is actually b.getService(), this is documented in many places. If you want field reference, you do b. <at> service. In the toString method, a field is used by default, as there is no need to go through the getter (which the class doesn't define anyways, it gets magically added later, but it doesn't influence anything).


So, I would say it is consistent and expected.


On Tue, Jun 26, 2012 at 8:24 PM, Sudhir N <sudhir_nimavat <at> yahoo.com> wrote:

so when the field is being referenced using the instance reference (b.service) groovy intelligently uses the metaClass property but not within the class.
Shouldn't this behavior be consistent ? 
 
   
Sudhir 
 

From: Wujek Srujek <wujek.srujek <at> gmail.com>
To: user <at> groovy.codehaus.org
Sent: Tuesday, 26 June 2012 11:51 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !

Try this:

    String toString() {
        return "${getService()} $title"
    }

I get "hello null"

In your toString, it's not the properties that get called, but the fields directly.

wujek

On Tue, Jun 26, 2012 at 7:56 PM, Sudhir N <sudhir_nimavat <at> yahoo.com> wrote:

See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 




Sudhir N | 26 Jun 2012 20:33
Picon
Favicon

Re: [groovy-user] Overriding properties using metaClass won't work within class !

Also when I have getService() property defined within class - I can use it as I am referencing the field - but opposite is not true. 
This is really a confusing behavior - I believe the language should be consistent no matter what way I reference a property I should always get the same result.

eg


 class Book {
    String title

    String getService({ return 'hello'}
    
    String toString({
       return "$service $title"
    }
}

Book new Book()
println b //prints hello null

Sudhir 
 

From: Sudhir N <sudhir_nimavat-/E1597aS9LQAvxtiuMwx3w@public.gmane.org>
To: "user-i9PBDF1N6cxnkHa44VUL00B+6BGkLq7r@public.gmane.org" <user-i9PBDF1N6cxnkHa44VUL08Xa4x6EXUF0@public.gmane.orgg>
Sent: Tuesday, 26 June 2012 11:54 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !


so when the field is being referenced using the instance reference (b.service) groovy intelligently uses the metaClass property but not within the class.
Shouldn't this behavior be consistent ? 
 
   
Sudhir 
 

From: Wujek Srujek <wujek.srujek <at> gmail.com>
To: user <at> groovy.codehaus.org
Sent: Tuesday, 26 June 2012 11:51 PM
Subject: Re: [groovy-user] Overriding properties using metaClass won't work within class !

Try this:

    String toString() {
        return "${getService()} $title"
    }

I get "hello null"

In your toString, it's not the properties that get called, but the fields directly.

wujek

On Tue, Jun 26, 2012 at 7:56 PM, Sudhir N <sudhir_nimavat-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:

See the example

class Book {
    String title
    def service
    
String toString({
 return "$service $title"
}
}

Book.metaClass.getService {-> return "hello"
Book new Book()

//This doesnt work either  - b. metaClass.getService {-> return "hello" 

println //prints null null
assert 'hello' == b.service​  //passes ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​ ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ 

 
   
So apparently when property is referenced within class it doesn't use the getter provided by metaClass.
Is there any solution to this !

 
Sudhir 
 






Gmane