26 Jun 2012 19:56
[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 b = new Book()
//This doesnt work either - b. metaClass.getService = {-> return "hello"}
println b //prints null null
assert 'hello' == b.service //passes
String title
def service
String toString() {
return "$service $title"
}
}
Book.metaClass.getService = {-> return "hello"}
Book b = new Book()
//This doesnt work either - b. metaClass.getService = {-> return "hello"}
println b //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
RSS Feed