replacing toString using Groovy metaprogramming
groovy, metaprogramming
Solution
See the first comment on this issue. It says about String's toString and other String related classes:
(...) seems to be intent, it is probably a good idea to have a faster invocation for classes that don't allow overriding toString().
Problem
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)?