環境は、Groovy 1.6.3 で、GroovyConsole で遊んでました。何なんだろ。
Groovy でクラスを宣言しそのインスタンスを生成した後に、クラスのメタクラスからメソッドを変更します。その後に、インスタンスのメタクラスを呼び出したら怒られた。
class Test { void print() { println 'Print' } } def t = new Test() t.print() // print Test.metaClass.print = { println 'Class Custom Print' } t.print() // Print new Test().print() // Class Custom Print t.metaClass.print = { println 'Instance Custom Print' } // Exception thrown: groovy.lang.MetaClassImpl cannot be cast to groovy.lang.GroovyObject // java.lang.ClassCastException: groovy.lang.MetaClassImpl cannot be cast to groovy.lang.GroovyObject