[ https://issues.apache.org/jira/browse/GROOVY-3942?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16343137#comment-16343137 ]
Jan Å mucr commented on GROOVY-3942: ----------------------------------- Just ran into this and trying to figure out some workaround. :( > Using metaClass to override methods in class hierarchy does not work as > expected > -------------------------------------------------------------------------------- > > Key: GROOVY-3942 > URL: https://issues.apache.org/jira/browse/GROOVY-3942 > Project: Groovy > Issue Type: Bug > Components: groovy-runtime > Affects Versions: 1.8-beta-1 > Environment: Windows XP > Reporter: Alex McManus > Priority: Major > Attachments: MetaclassOverrideTest.groovy > > > I've found some strange behaviour when dynamically overriding methods that > are called in a class hierarchy, which I think might be a bug. The problem is > that when I override a method on a subclass, code in the superclass still > calls the original method (note all classes are Groovy ones, I know this > wouldn't work with a Java class hierarchy). > I've isolated it to this sample code: > {code:Java} > class BaseClass { > def baseClassMethod() { > println "BaseClass.baseClassMethod()" > internalMethod() > } > def internalMethod() { > println " BaseClass.internalMethod()" > } > } > class SubClass extends BaseClass { > def subClassMethod() { > println "SubClass.subClassMethod()" > internalMethod() > } > } > def subClass = new SubClass() > subClass.metaClass.internalMethod = { -> println (" > (dynamic).internalMethod()")} > subClass.baseClassMethod() > subClass.subClassMethod() > {code} > ...which gives me the following output... > BaseClass.baseClassMethod() > BaseClass.internalMethod() > SubClass.subClassMethod() > (dynamic).internalMethod() > I would have expected that the dynamic version of internalMethod() would be > called both times, given that Groovy has dynamic method dispatching. > Some discussion of this issue took place on the mailing list: > http://old.nabble.com/Problems-using-metaClass-to-override-methods-in-class-hierarchy-td26743895.html#a26743895 > The suggestion is that the CallSiteArray.createCallCurrentSite(...) method > could check whether the receiver is a sub class of the sender class. -- This message was sent by Atlassian JIRA (v7.6.3#76005)