replacing toString using Groovy metaprogramming
Posted
by Don
on Stack Overflow
See other posts from Stack Overflow
or by Don
Published on 2010-05-19T10:13:16Z
Indexed on
2010/05/19
13:50 UTC
Read the original article
Hit count: 291
groovy
|metaprogramming
In the following Groovy snippet, I attempt to replace both the hashCode
and toString
methods
String.metaClass.toString = {-> "override" }
String.metaClass.hashCode = {-> 22 }
But when I test it out, only the replacement of hashCode
works
String s = "foo"
println s.hashCode() // prints 22
println s.toString() // prints "foo"
Is toString
somehow a special case (possibly for security reasons)?
© Stack Overflow or respective owner