不能替换静态方法

Cannot replace static method

我正在尝试使用 MetaClass 在运行时替换静态方法。

class MyClass {
    private static String getString() {
        'Hello'
    }

    String testString

    MyClass() {
        testString = getString()
    }
}

MyClass.metaClass.static.getString = { ->
    'Hello world!'
}

def test = new MyClass()
assert test.testString == 'Hello world!'

然而这不起作用。

Caught: Assertion failed: 

assert test.testString == 'Hello world!'
       |    |          |
       |    Hello      false
       MyClass@5876a9af

由于 bug in Groovy 2.4.3, it is not possible to change private methods via metaclass. I've changed the method to public (well, default) and also changed the constructor so that it explicitly calls it's class' getString method and now it seems to be working in Groovy web console

编辑后的完整代码:

class MyClass {
    static String getString() {
        'Hello'
    }
    String testString
    MyClass() {
        testString = MyClass.getString()
    }
}
MyClass.metaClass.static.getString = {'Hello world!'}
def test = new MyClass()
assert test.testString == 'Hello world!'