groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yasin Diouri <amet...@gmail.com>
Subject Replaced static method not replaced only when called from constructor
Date Fri, 08 Apr 2016 20:54:53 GMT
Hello!

I have a strange issue which I already posted on StackOverflow
<http://stackoverflow.com/questions/36501291/replaced-static-method-not-replaced-only-when-called-from-constructor>
but I was advised to send it to this mailing list as well.

class ClassA {
    static String getA() {
        'A'
    }

    String string

    ClassA() {
        println getA()
        println ClassA.getA()
        string = getA()
    }}

// MockingClassA.metaClass.static.getA = { -> 'B'
}ClassA.metaClass.getA = { -> 'B' }assert ClassA.getA() == 'B'
def test = new ClassA()assert test.getA() == 'B'assert test.string == 'B'

Resuming: calling getA() from anywhere but the constructor calls the
replaced implementation. To be able to call the new implementation from the
constructor you have to use the long form ClassA.getA().

Kind regards,
Yassine Diouri

Mime
View raw message