You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Bruno Bowden (JIRA)" <ji...@apache.org> on 2015/06/30 21:38:05 UTC

[jira] [Commented] (GROOVY-3493) Cannot override methods via metaclass that are part of an interface implementation

    [ https://issues.apache.org/jira/browse/GROOVY-3493?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14608937#comment-14608937 ] 

Bruno Bowden commented on GROOVY-3493:
--------------------------------------

Workaround to override interface methods via metaclass. Similar to what toddg reported back in 2009. Here's a working example that can be used to workaround this. Note that this modifies the class rather than a specific instance, which the original reporter was trying to do.

metaclass modification BEFORE construction:

{code}
interface I {
    def doIt()
}

class T implements I {
    def doIt() { true }
}

I.metaClass.doIt = { -> false }
T t = new T()
assert !t.doIt()
{code}

metaclass modification AFTER construction:

{code}
interface I {
    def doIt()
}

class T implements I {
    def doIt() { true }
}

T t = new T()
// Removing either of the following two lines breaks this
I.metaClass.doIt = { -> false }
t.metaClass.doIt = { -> false }
assert !t.doIt()
{code}

> Cannot override methods via metaclass that are part of an interface implementation
> ----------------------------------------------------------------------------------
>
>                 Key: GROOVY-3493
>                 URL: https://issues.apache.org/jira/browse/GROOVY-3493
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-runtime
>    Affects Versions: 1.6.2
>            Reporter: Luke Daley
>            Priority: Critical
>
> The following works...
> {code}
> class T {
>        def doIt() { true }
> }
> def t = new T()
> assert t.doIt()
> t.metaClass.doIt = { -> false }
> assert !t.doIt()
> {code}
> But this fails...
> {code}
> interface I {
>        def doIt()
> }
> class T implements I {
>        def doIt() { true }
> }
> def t = new T()
> assert t.doIt()
> t.metaClass.doIt = { -> false }
> assert !t.doIt()
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)