First, when you add Fizz to the main class, its instances do not receive the method, since the instances have already been counted and added to memory.
Thus, one way to approach this is to use the method signature from the source class. Therefore, instead of
fizz.doStuff(blah)
call the class method. therefore
fizz.&doStuff(blah)
Gets the method signature from the source class, but uses the attributes from the instance. However, as you can imagine, since it calls the calling class, this is a bit of a difficult call.
Now, one alternative to popping each instance is to make instances of ExpandoMetaClass instances for Fizz. Hence,...
Fizz.metaClass.doStuff = {return "blah"} fizz = new Fizz() Fizz.metaClass.doOtherStuff = {return "more blah"} assert fizz.doOtherStuff() == "more blah"
Hope this helps
UPDATE:
Full code example:
class Fizz{ } Fizz.metaClass.doOtherStuff = {return "more blah"} def fizz = new Fizz() assert fizz.doOtherStuff() == "more blah" def fizz1 = new Fizz() assert fizz1.doOtherStuff() == "more blah"
source share